renamed ActiveRecord::create() to populateRecord()

- refactored elasticsearch AR to make type and index available in
  instantiate(), issue #1313

fixes #2281
This commit is contained in:
Carsten Brandt
2014-02-03 14:01:29 +01:00
parent dc4feef716
commit 16e1aff41b
15 changed files with 65 additions and 40 deletions

View File

@@ -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 #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 #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 `ActiveRecord::getPopulatedRelations()` to `getRelatedRecords()` (qiangxue)
- Chg: Renamed `attributeName` and `className` to `targetAttribute` and `targetClass` for `UniqueValidator` and `ExistValidator` (qiangxue)

View File

@@ -144,7 +144,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface
} else {
/** @var ActiveRecord $class */
$class = $this->modelClass;
$model = $class::create($row);
$model = $class::instantiate($row);
$class::populateRecord($model, $row);
}
if (!empty($this->with)) {
$models = [$model];

View File

@@ -128,11 +128,14 @@ trait ActiveQueryTrait
$class = $this->modelClass;
if ($this->indexBy === null) {
foreach ($rows as $row) {
$models[] = $class::create($row);
$model = $class::instantiate($row);
$class::populateRecord($model, $row);
$models[] = $model;
}
} else {
foreach ($rows as $row) {
$model = $class::create($row);
$model = $class::instantiate($row);
$class::populateRecord($model, $row);
if (is_string($this->indexBy)) {
$key = $model->{$this->indexBy};
} else {

View File

@@ -277,9 +277,9 @@ class ActiveRecord extends BaseActiveRecord
/**
* @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());
$columns = static::getTableSchema()->columns;
foreach ($row as $name => $value) {
@@ -293,7 +293,6 @@ class ActiveRecord extends BaseActiveRecord
}
}
$record->setOldAttributes($record->getAttributes());
return $record;
}
/**

View File

@@ -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.
*
* It is an internal method meant to be called to create active record objects after
* This 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
* the query results into Active Records.
* the query results into active records.
*
* When calling this method manually you should call [[afterFind()]] on the created
* 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)
* @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());
foreach ($row as $name => $value) {
if (isset($columns[$name])) {
@@ -1010,12 +1008,13 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
}
}
$record->_oldAttributes = $record->_attributes;
return $record;
}
/**
* 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
* depends on the row data to be populated into the record.
* For example, by creating a record based on the value of a column,