Commit 16e1aff4 by Carsten Brandt

renamed ActiveRecord::create() to populateRecord()

- refactored elasticsearch AR to make type and index available in instantiate(), issue #1313 fixes #2281
parent dc4feef7
...@@ -152,7 +152,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -152,7 +152,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface
} else { } else {
/** @var ActiveRecord $class */ /** @var ActiveRecord $class */
$class = $this->modelClass; $class = $this->modelClass;
$model = $class::create($result); $model = $class::instantiate($result);
$class::populateRecord($model, $result);
} }
if (!empty($this->with)) { if (!empty($this->with)) {
$models = [$model]; $models = [$model];
......
...@@ -94,7 +94,8 @@ class ActiveRecord extends BaseActiveRecord ...@@ -94,7 +94,8 @@ class ActiveRecord extends BaseActiveRecord
$command = static::getDb()->createCommand(); $command = static::getDb()->createCommand();
$result = $command->get(static::index(), static::type(), $primaryKey, $options); $result = $command->get(static::index(), static::type(), $primaryKey, $options);
if ($result['exists']) { if ($result['exists']) {
$model = static::create($result); $model = static::instantiate($result);
static::populateRecord($model, $result);
$model->afterFind(); $model->afterFind();
return $model; return $model;
} }
...@@ -123,7 +124,8 @@ class ActiveRecord extends BaseActiveRecord ...@@ -123,7 +124,8 @@ class ActiveRecord extends BaseActiveRecord
$models = []; $models = [];
foreach($result['docs'] as $doc) { foreach($result['docs'] as $doc) {
if ($doc['exists']) { if ($doc['exists']) {
$model = static::create($doc); $model = static::instantiate($doc);
static::populateRecord($model, $doc);
$model->afterFind(); $model->afterFind();
$models[] = $model; $models[] = $model;
} }
...@@ -264,22 +266,38 @@ class ActiveRecord extends BaseActiveRecord ...@@ -264,22 +266,38 @@ class ActiveRecord extends BaseActiveRecord
} }
/** /**
* Creates an active record object using a row of data. * @inheritdoc
* This method is called by [[ActiveQuery]] to populate the query results
* into Active Records. It is not meant to be used to create new records.
* @param array $row attribute values (name => value)
* @return ActiveRecord the newly created active record.
*/ */
public static function create($row) public static function populateRecord($record, $row)
{ {
$record = parent::create($row['_source']); parent::populateRecord($record, $row['_source']);
$pk = static::primaryKey()[0]; $pk = static::primaryKey()[0];
if ($pk === '_id') { if ($pk === '_id') {
$record->$pk = $row['_id']; $record->_id = $row['_id'];
} }
$record->_score = isset($row['_score']) ? $row['_score'] : null; $record->_score = isset($row['_score']) ? $row['_score'] : null;
$record->_version = isset($row['_version']) ? $row['_version'] : null; // TODO version should always be available... $record->_version = isset($row['_version']) ? $row['_version'] : null; // TODO version should always be available...
return $record; }
/**
* Creates an active record instance.
*
* This method is called together with [[populateRecord()]] by [[ActiveQuery]].
*
* You may override this method if the instance being created
* depends on the row data to be populated into the record.
* For example, by creating a record based on the value of a column,
* you may implement the so-called single-table inheritance mapping.
* @param array $row row data to be populated into the record.
* This array consists of the following keys:
* - `_source`: refers to the attributes of the record.
* - `_type`: the type this record is stored in.
* - `_index`: the index this record is stored in.
* @return static the newly created active record
*/
public static function instantiate($row)
{
return new static;
} }
/** /**
......
...@@ -5,9 +5,11 @@ Yii Framework 2 elasticsearch extension Change Log ...@@ -5,9 +5,11 @@ Yii Framework 2 elasticsearch extension Change Log
---------------------------- ----------------------------
- Bug #1993: afterFind event in AR is now called after relations have been populated (cebe, creocoder) - Bug #1993: afterFind event in AR is now called after relations have been populated (cebe, creocoder)
- Enh #1313: made index and type available in `ActiveRecord::instantiate()` to allow creating records based on elasticsearch type when doing cross index/type search (cebe)
- Enh #1382: Added a debug toolbar panel for elasticsearch (cebe) - Enh #1382: Added a debug toolbar panel for elasticsearch (cebe)
- Enh #1765: Added support for primary key path mapping, pk can now be part of the attributes when mapping is defined (cebe) - Enh #1765: Added support for primary key path mapping, pk can now be part of the attributes when mapping is defined (cebe)
- Chg #1765: Changed handling of ActiveRecord primary keys, removed getId(), use getPrimaryKey() instead (cebe) - Chg #1765: Changed handling of ActiveRecord primary keys, removed getId(), use getPrimaryKey() instead (cebe)
- Chg #2281: Renamed `ActiveRecord::create()` to `populateRecord()` and changed signature. This method will not call instantiate() anymore (cebe)
2.0.0 alpha, December 1, 2013 2.0.0 alpha, December 1, 2013
----------------------------- -----------------------------
......
...@@ -81,7 +81,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -81,7 +81,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface
} else { } else {
/** @var ActiveRecord $class */ /** @var ActiveRecord $class */
$class = $this->modelClass; $class = $this->modelClass;
$model = $class::create($row); $model = $class::instantiate($row);
$class::populateRecord($model, $row);
} }
if (!empty($this->with)) { if (!empty($this->with)) {
$models = [$model]; $models = [$model];
......
...@@ -81,7 +81,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -81,7 +81,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface
} else { } else {
/** @var ActiveRecord $class */ /** @var ActiveRecord $class */
$class = $this->modelClass; $class = $this->modelClass;
$model = $class::create($row); $model = $class::instantiate($row);
$class::populateRecord($model, $row);
} }
if (!empty($this->with)) { if (!empty($this->with)) {
$models = [$model]; $models = [$model];
......
...@@ -112,7 +112,8 @@ class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface ...@@ -112,7 +112,8 @@ class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface
} else { } else {
/** @var ActiveRecord $class */ /** @var ActiveRecord $class */
$class = $this->modelClass; $class = $this->modelClass;
$model = $class::create($row); $model = $class::instantiate($row);
$class::populateRecord($model, $row);
} }
if (!empty($this->with)) { if (!empty($this->with)) {
$models = [$model]; $models = [$model];
......
...@@ -6,6 +6,7 @@ Yii Framework 2 redis extension Change Log ...@@ -6,6 +6,7 @@ Yii Framework 2 redis extension Change Log
- Bug #1993: afterFind event in AR is now called after relations have been populated (cebe, creocoder) - Bug #1993: afterFind event in AR is now called after relations have been populated (cebe, creocoder)
- Enh #1773: keyPrefix property of Session and Cache is not restricted to alnum characters anymore (cebe) - Enh #1773: keyPrefix property of Session and Cache is not restricted to alnum characters anymore (cebe)
- Chg #2281: Renamed `ActiveRecord::create()` to `populateRecord()` and changed signature. This method will not call instantiate() anymore (cebe)
2.0.0 alpha, December 1, 2013 2.0.0 alpha, December 1, 2013
----------------------------- -----------------------------
......
...@@ -139,7 +139,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -139,7 +139,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface
} else { } else {
/** @var $class ActiveRecord */ /** @var $class ActiveRecord */
$class = $this->modelClass; $class = $this->modelClass;
$model = $class::create($row); $model = $class::instantiate($row);
$class::populateRecord($model, $row);
} }
if (!empty($this->with)) { if (!empty($this->with)) {
$models = [$model]; $models = [$model];
......
...@@ -619,15 +619,11 @@ abstract class ActiveRecord extends BaseActiveRecord ...@@ -619,15 +619,11 @@ abstract class ActiveRecord extends BaseActiveRecord
} }
/** /**
* Creates an active record object using a row of data. * @inheritdoc
* This method is called by [[ActiveQuery]] to populate the query results
* into Active Records. It is not meant to be used to create new records.
* @param array $row attribute values (name => value)
* @return ActiveRecord the newly created active record.
*/ */
public static function create($row) public static function populateRecord($record, $row)
{ {
$record = static::instantiate($row); // TODO refactor to call parent
$columns = static::getIndexSchema()->columns; $columns = static::getIndexSchema()->columns;
foreach ($row as $name => $value) { foreach ($row as $name => $value) {
if (isset($columns[$name])) { if (isset($columns[$name])) {
...@@ -641,7 +637,6 @@ abstract class ActiveRecord extends BaseActiveRecord ...@@ -641,7 +637,6 @@ abstract class ActiveRecord extends BaseActiveRecord
} }
} }
$record->setOldAttributes($record->getAttributes()); $record->setOldAttributes($record->getAttributes());
return $record;
} }
/** /**
......
...@@ -7,6 +7,7 @@ Yii Framework 2 sphinx extension Change Log ...@@ -7,6 +7,7 @@ Yii Framework 2 sphinx extension Change Log
- Bug #1993: afterFind event in AR is now called after relations have been populated (cebe, creocoder) - Bug #1993: afterFind event in AR is now called after relations have been populated (cebe, creocoder)
- Bug #2160: SphinxQL does not support OFFSET (qiangxue, romeo7) - Bug #2160: SphinxQL does not support OFFSET (qiangxue, romeo7)
- Enh #1398: Refactor ActiveRecord to use BaseActiveRecord class of the framework (klimov-paul) - Enh #1398: Refactor ActiveRecord to use BaseActiveRecord class of the framework (klimov-paul)
- Chg #2281: Renamed `ActiveRecord::create()` to `populateRecord()` and changed signature. This method will not call instantiate() anymore (cebe)
2.0.0 alpha, December 1, 2013 2.0.0 alpha, December 1, 2013
----------------------------- -----------------------------
......
...@@ -142,6 +142,7 @@ Yii Framework 2 Change Log ...@@ -142,6 +142,7 @@ Yii Framework 2 Change Log
- Chg #2175: QueryBuilder will now append UNION statements at the end of the primary SQL (qiangxue) - Chg #2175: QueryBuilder will now append UNION statements at the end of the primary SQL (qiangxue)
- Chg #2210: Mysql driver will now treat `tinyint(1)` as integer instead of boolean (qiangxue) - Chg #2210: Mysql driver will now treat `tinyint(1)` as integer instead of boolean (qiangxue)
- Chg #2248: Renamed `yii\base\Model::DEFAULT_SCENARIO` to `yii\base\Model::SCENARIO_DEFAULT` (samdark) - Chg #2248: Renamed `yii\base\Model::DEFAULT_SCENARIO` to `yii\base\Model::SCENARIO_DEFAULT` (samdark)
- Chg #2281: Renamed `ActiveRecord::create()` to `populateRecord()` and changed signature. This method will not call instantiate() anymore (cebe)
- Chg: Renamed `yii\jui\Widget::clientEventsMap` to `clientEventMap` (qiangxue) - Chg: Renamed `yii\jui\Widget::clientEventsMap` to `clientEventMap` (qiangxue)
- Chg: Renamed `ActiveRecord::getPopulatedRelations()` to `getRelatedRecords()` (qiangxue) - Chg: Renamed `ActiveRecord::getPopulatedRelations()` to `getRelatedRecords()` (qiangxue)
- Chg: Renamed `attributeName` and `className` to `targetAttribute` and `targetClass` for `UniqueValidator` and `ExistValidator` (qiangxue) - Chg: Renamed `attributeName` and `className` to `targetAttribute` and `targetClass` for `UniqueValidator` and `ExistValidator` (qiangxue)
......
...@@ -144,7 +144,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -144,7 +144,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface
} else { } else {
/** @var ActiveRecord $class */ /** @var ActiveRecord $class */
$class = $this->modelClass; $class = $this->modelClass;
$model = $class::create($row); $model = $class::instantiate($row);
$class::populateRecord($model, $row);
} }
if (!empty($this->with)) { if (!empty($this->with)) {
$models = [$model]; $models = [$model];
......
...@@ -128,11 +128,14 @@ trait ActiveQueryTrait ...@@ -128,11 +128,14 @@ trait ActiveQueryTrait
$class = $this->modelClass; $class = $this->modelClass;
if ($this->indexBy === null) { if ($this->indexBy === null) {
foreach ($rows as $row) { foreach ($rows as $row) {
$models[] = $class::create($row); $model = $class::instantiate($row);
$class::populateRecord($model, $row);
$models[] = $model;
} }
} else { } else {
foreach ($rows as $row) { foreach ($rows as $row) {
$model = $class::create($row); $model = $class::instantiate($row);
$class::populateRecord($model, $row);
if (is_string($this->indexBy)) { if (is_string($this->indexBy)) {
$key = $model->{$this->indexBy}; $key = $model->{$this->indexBy};
} else { } else {
......
...@@ -277,9 +277,9 @@ class ActiveRecord extends BaseActiveRecord ...@@ -277,9 +277,9 @@ class ActiveRecord extends BaseActiveRecord
/** /**
* @inheritdoc * @inheritdoc
*/ */
public static function create($row) public static function populateRecord($record, $row)
{ {
$record = static::instantiate($row); // TODO refactor to call parent
$attributes = array_flip($record->attributes()); $attributes = array_flip($record->attributes());
$columns = static::getTableSchema()->columns; $columns = static::getTableSchema()->columns;
foreach ($row as $name => $value) { foreach ($row as $name => $value) {
...@@ -293,7 +293,6 @@ class ActiveRecord extends BaseActiveRecord ...@@ -293,7 +293,6 @@ class ActiveRecord extends BaseActiveRecord
} }
} }
$record->setOldAttributes($record->getAttributes()); $record->setOldAttributes($record->getAttributes());
return $record;
} }
/** /**
......
...@@ -984,23 +984,21 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -984,23 +984,21 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
} }
/** /**
* Creates an active record object using a row of data from the database/storage. * Populates an active record object using a row of data from the database/storage.
* *
* This method is *not* meant to be used to create new records. * This is an internal method meant to be called to create active record objects after
*
* It is an internal method meant to be called to create active record objects after
* fetching data from the database. It is mainly used by [[ActiveQuery]] to populate * fetching data from the database. It is mainly used by [[ActiveQuery]] to populate
* the query results into Active Records. * the query results into active records.
* *
* When calling this method manually you should call [[afterFind()]] on the created * When calling this method manually you should call [[afterFind()]] on the created
* record to trigger the [[EVENT_AFTER_FIND|afterFind Event]]. * record to trigger the [[EVENT_AFTER_FIND|afterFind Event]].
* *
* @param BaseActiveRecord $record the record to be populated. In most cases this will be an instance
* created by [[instantiate()]] beforehand.
* @param array $row attribute values (name => value) * @param array $row attribute values (name => value)
* @return static the newly created active record.
*/ */
public static function create($row) public static function populateRecord($record, $row)
{ {
$record = static::instantiate($row);
$columns = array_flip($record->attributes()); $columns = array_flip($record->attributes());
foreach ($row as $name => $value) { foreach ($row as $name => $value) {
if (isset($columns[$name])) { if (isset($columns[$name])) {
...@@ -1010,12 +1008,13 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -1010,12 +1008,13 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
} }
} }
$record->_oldAttributes = $record->_attributes; $record->_oldAttributes = $record->_attributes;
return $record;
} }
/** /**
* Creates an active record instance. * Creates an active record instance.
* This method is called by [[create()]]. *
* This method is called together with [[populateRecord()]] by [[ActiveQuery]].
*
* You may override this method if the instance being created * You may override this method if the instance being created
* depends on the row data to be populated into the record. * depends on the row data to be populated into the record.
* For example, by creating a record based on the value of a column, * For example, by creating a record based on the value of a column,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment