@@ -27,7 +27,7 @@ class ActiveExcelSheet extends ExcelSheet
27
27
28
28
protected $ _query ;
29
29
protected $ _attributes ;
30
- protected $ _columnTypes ;
30
+ protected $ _columnSchemas ;
31
31
protected $ _modelInstance ;
32
32
33
33
/**
@@ -131,18 +131,21 @@ public function getFormats()
131
131
if ($ this ->_formats === null ) {
132
132
$ this ->_formats = [];
133
133
$ attrs = $ this ->normalizeIndex ($ this ->getAttributes ());
134
- $ types = $ this ->normalizeIndex ($ this ->getColumnTypes ());
134
+ $ schemas = $ this ->normalizeIndex ($ this ->getColumnSchemas ());
135
135
foreach ($ attrs as $ c => $ attr ) {
136
- switch ($ types [$ c ]->type ) {
136
+ if (!isset ($ schemas [$ c ])) {
137
+ continue ;
138
+ }
139
+ switch ($ schemas [$ c ]->type ) {
137
140
case 'date ' :
138
141
$ this ->_formats [$ c ] = $ this ->dateFormat ;
139
142
break ;
140
143
case 'datetime ' :
141
144
$ this ->_formats [$ c ] = $ this ->dateTimeFormat ;
142
145
break ;
143
146
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 ' );
146
149
$ this ->_formats [$ c ] = $ decimals .$ zeroPad ;
147
150
break ;
148
151
}
@@ -181,9 +184,12 @@ public function getFormatters()
181
184
if ($ this ->_formatters === null ) {
182
185
$ this ->_formatters = [];
183
186
$ attrs = $ this ->normalizeIndex ($ this ->getAttributes ());
184
- $ types = $ this ->normalizeIndex ($ this ->getColumnTypes ());
187
+ $ schemas = $ this ->normalizeIndex ($ this ->getColumnSchemas ());
185
188
foreach ($ attrs as $ c => $ attr ) {
186
- switch ($ types [$ c ]->type ) {
189
+ if (!isset ($ schemas [$ c ])) {
190
+ continue ;
191
+ }
192
+ switch ($ schemas [$ c ]->type ) {
187
193
case 'date ' :
188
194
$ this ->_formatters [$ c ] = function ($ v ) {
189
195
if (empty ($ v )) {
@@ -271,18 +277,20 @@ protected static function getRelatedModelInstance($model, $name)
271
277
}
272
278
273
279
/**
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.
276
282
*/
277
- protected function getColumnTypes ()
283
+ protected function getColumnSchemas ()
278
284
{
279
- if ($ this ->_columnTypes === null ) {
285
+ if ($ this ->_columnSchemas === null ) {
280
286
$ 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 );
283
289
}, $ this ->getAttributes ());
290
+ // Filter out null values
291
+ $ this ->_columnSchemas = array_filter ($ schemas );
284
292
}
285
- return $ this ->_columnTypes ;
293
+ return $ this ->_columnSchemas ;
286
294
}
287
295
288
296
/**
@@ -350,19 +358,21 @@ protected function toExcelTime($value)
350
358
* @param mixed $isRelation whether the name specifies a relation, in which
351
359
* case an `ActiveRecord` is returned. Default is `false`, which returns a
352
360
* `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)
355
364
*/
356
- public static function getType ($ model , $ attribute , $ isRelation = false )
365
+ public static function getSchema ($ model , $ attribute , $ isRelation = false )
357
366
{
358
367
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 );
360
369
$ attribute = substr ($ attribute , $ pos + 1 );
361
370
}
362
371
if ($ isRelation ) {
363
372
return self ::getRelatedModelInstance ($ model , $ attribute );
364
373
} else {
365
- return $ model ->getTableSchema ()->columns [$ attribute ];
374
+ $ columnSchemas = $ model ->getTableSchema ()->columns ;
375
+ return isset ($ columnSchemas [$ attribute ]) ? $ columnSchemas [$ attribute ] : null ;
366
376
}
367
377
}
368
378
}
0 commit comments