Skip to content

Commit 6810b81

Browse files
committed
Make modelInstance configurable
1 parent fdc172f commit 6810b81

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Property | Description
9898
`dateFormat` | The excel format to use for `date` DB types. Default is `dd/mm/yyyy`.
9999
`dateTimeFormat` | The excel format to use for `datetime` DB types. Default is `dd/mm/yyyy hh:mm:ss`.
100100
`batchSize` | The query batchsize to use. Default is `100`.
101+
`modelInstance` (optional) | The query's `modelClass` instance used to obtain attribute types and titles. If not set an instance of the query's `modelClass` is created automatically.
101102

102103
> **Note** Since version 2.3.1 datetime attributes will automatically be
103104
> converted to the correct timezone. This feature makes use of the current

src/ActiveExcelSheet.php

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ActiveExcelSheet extends ExcelSheet
2828
protected $_query;
2929
protected $_attributes;
3030
protected $_columnTypes;
31+
protected $_modelInstance;
3132

3233
/**
3334
* @return yii\db\ActiveQuery the query for the sheet data
@@ -64,9 +65,7 @@ public function getData()
6465
public function getAttributes()
6566
{
6667
if ($this->_attributes === null) {
67-
$class = $this->getQuery()->modelClass;
68-
$model = new $class;
69-
$this->_attributes = $model->attributes();
68+
$this->_attributes = $this->getModelInstance()->attributes();
7069
}
7170
return $this->_attributes;
7271
}
@@ -94,8 +93,7 @@ public function setData($value)
9493
public function getTitles()
9594
{
9695
if ($this->_titles === null) {
97-
$class = $this->getQuery()->modelClass;
98-
$model = new $class;
96+
$model = $this->getModelInstance();
9997
$this->_titles = array_map(function ($a) use ($model) {
10098
return $model->getAttributeLabel($a);
10199
}, $this->getAttributes());
@@ -239,15 +237,47 @@ public function setFormatters($value)
239237
}
240238
}
241239

240+
/**
241+
* @return yii\db\ActiveRecord an instance of the main model on which the
242+
* query is performed on. This is used to obtain column titles and types.
243+
*/
244+
public function getModelInstance()
245+
{
246+
if ($this->_modelInstance === null) {
247+
$class = $this->getQuery()->modelClass;
248+
$this->_modelInstance = new $class;
249+
}
250+
return $this->_modelInstance;
251+
}
252+
253+
/**
254+
* @param yii\db\ActiveRecord $model an instance of the main model on which
255+
* the query is performed on. This is used to obtain column titles and
256+
* types.
257+
*/
258+
public function setModelInstance($model)
259+
{
260+
$this->_modelInstance = $model;
261+
}
262+
263+
/**
264+
* @return yii\db\ActiveRecord a new instance of a related model for the
265+
* given model. This is used to obtain column types.
266+
*/
267+
protected function getRelatedModelInstance($model, $name)
268+
{
269+
$class = $model->getRelation($name)->modelClass;
270+
return new $class;
271+
}
272+
242273
/**
243274
* @return yii\db\ColumnSchema[] the DB column types `ColumnSchema::$type`
244275
* indexed by 0-based column index
245276
*/
246277
protected function getColumnTypes()
247278
{
248279
if ($this->_columnTypes === null) {
249-
$class = $this->getQuery()->modelClass;
250-
$model = new $class;
280+
$model = $this->getModelInstance();
251281
$this->_columnTypes = array_map(function ($attr) use ($model) {
252282
return self::getType($model, $attr);
253283
}, $this->getAttributes());
@@ -330,8 +360,7 @@ public static function getType($model, $attribute, $isRelation = false)
330360
$attribute = substr($attribute, $pos + 1);
331361
}
332362
if ($isRelation) {
333-
$class = $model->getRelation($attribute)->modelClass;
334-
return new $class;
363+
return $this->getRelatedModelInstance($model, $attribute);
335364
} else {
336365
return $model->getTableSchema()->columns[$attribute];
337366
}

0 commit comments

Comments
 (0)