@@ -27,7 +27,7 @@ class ActiveExcelSheet extends ExcelSheet
2727
2828 protected $ _query ;
2929 protected $ _attributes ;
30- protected $ _columnTypes ;
30+ protected $ _columnSchemas ;
3131 protected $ _modelInstance ;
3232
3333 /**
@@ -131,18 +131,21 @@ public function getFormats()
131131 if ($ this ->_formats === null ) {
132132 $ this ->_formats = [];
133133 $ attrs = $ this ->normalizeIndex ($ this ->getAttributes ());
134- $ types = $ this ->normalizeIndex ($ this ->getColumnTypes ());
134+ $ schemas = $ this ->normalizeIndex ($ this ->getColumnSchemas ());
135135 foreach ($ attrs as $ c => $ attr ) {
136- switch ($ types [$ c ]->type ) {
136+ if (!isset ($ schemas [$ c ])) {
137+ continue ;
138+ }
139+ switch ($ schemas [$ c ]->type ) {
137140 case 'date ' :
138141 $ this ->_formats [$ c ] = $ this ->dateFormat ;
139142 break ;
140143 case 'datetime ' :
141144 $ this ->_formats [$ c ] = $ this ->dateTimeFormat ;
142145 break ;
143146 case 'decimal ' :
144- $ decimals = str_pad ('#, ' , $ types [$ c ]->scale , '# ' );
145- $ zeroPad = str_pad ('0. ' , $ types [$ c ]->scale , '0 ' );
147+ $ decimals = str_pad ('#, ' , $ schemas [$ c ]->scale , '# ' );
148+ $ zeroPad = str_pad ('0. ' , $ schemas [$ c ]->scale , '0 ' );
146149 $ this ->_formats [$ c ] = $ decimals .$ zeroPad ;
147150 break ;
148151 }
@@ -181,9 +184,12 @@ public function getFormatters()
181184 if ($ this ->_formatters === null ) {
182185 $ this ->_formatters = [];
183186 $ attrs = $ this ->normalizeIndex ($ this ->getAttributes ());
184- $ types = $ this ->normalizeIndex ($ this ->getColumnTypes ());
187+ $ schemas = $ this ->normalizeIndex ($ this ->getColumnSchemas ());
185188 foreach ($ attrs as $ c => $ attr ) {
186- switch ($ types [$ c ]->type ) {
189+ if (!isset ($ schemas [$ c ])) {
190+ continue ;
191+ }
192+ switch ($ schemas [$ c ]->type ) {
187193 case 'date ' :
188194 $ this ->_formatters [$ c ] = function ($ v ) {
189195 if (empty ($ v )) {
@@ -271,18 +277,20 @@ protected static function getRelatedModelInstance($model, $name)
271277 }
272278
273279 /**
274- * @return yii\db\ColumnSchema[] the DB column types `ColumnSchema::$type`
275- * indexed by 0-based column index
280+ * @return yii\db\ColumnSchema[] the DB column schemas indexed by 0-based
281+ * column index. This only includes columns for which a DB schema exists.
276282 */
277- protected function getColumnTypes ()
283+ protected function getColumnSchemas ()
278284 {
279- if ($ this ->_columnTypes === null ) {
285+ if ($ this ->_columnSchemas === null ) {
280286 $ model = $ this ->getModelInstance ();
281- $ this -> _columnTypes = array_map (function ($ attr ) use ($ model ) {
282- return self ::getType ($ model , $ attr );
287+ $ schemas = array_map (function ($ attr ) use ($ model ) {
288+ return self ::getSchema ($ model , $ attr );
283289 }, $ this ->getAttributes ());
290+ // Filter out null values
291+ $ this ->_columnSchemas = array_filter ($ schemas );
284292 }
285- return $ this ->_columnTypes ;
293+ return $ this ->_columnSchemas ;
286294 }
287295
288296 /**
@@ -350,19 +358,21 @@ protected function toExcelTime($value)
350358 * @param mixed $isRelation whether the name specifies a relation, in which
351359 * case an `ActiveRecord` is returned. Default is `false`, which returns a
352360 * `ColumnSchema`.
353- * @return yii\db\ColumnSchema|yii\db\ActiveRecord the type instance of the
354- * attribute
361+ * @return yii\db\ColumnSchema|yii\db\ActiveRecord|null the type instance
362+ * of the attribute or `null` if the attribute is not a DB column (e.g.
363+ * public property or defined by getter)
355364 */
356- public static function getType ($ model , $ attribute , $ isRelation = false )
365+ public static function getSchema ($ model , $ attribute , $ isRelation = false )
357366 {
358367 if (($ pos = strrpos ($ attribute , '. ' )) !== false ) {
359- $ model = self ::getType ($ model , substr ($ attribute , 0 , $ pos ), true );
368+ $ model = self ::getSchema ($ model , substr ($ attribute , 0 , $ pos ), true );
360369 $ attribute = substr ($ attribute , $ pos + 1 );
361370 }
362371 if ($ isRelation ) {
363372 return self ::getRelatedModelInstance ($ model , $ attribute );
364373 } else {
365- return $ model ->getTableSchema ()->columns [$ attribute ];
374+ $ columnSchemas = $ model ->getTableSchema ()->columns ;
375+ return isset ($ columnSchemas [$ attribute ]) ? $ columnSchemas [$ attribute ] : null ;
366376 }
367377 }
368378}
0 commit comments