Commit a2fe1284 by Qiang Xue

refactored query and relation.

parent 684ee633
......@@ -68,7 +68,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
if (!empty($rows)) {
$models = $this->createModels($rows);
if (!empty($this->with)) {
$this->populateRelations($models, $this->with);
$this->findWith($this->with, $models);
}
return $models;
} else {
......@@ -98,7 +98,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
}
if (!empty($this->with)) {
$models = [$model];
$this->populateRelations($models, $this->with);
$this->findWith($this->with, $models);
$model = $models[0];
}
return $model;
......
......@@ -21,7 +21,7 @@ trait ActiveQueryTrait
*/
public $modelClass;
/**
* @var array list of relations that this query should be performed with
* @var array a list of relations that this query should be performed with
*/
public $with;
/**
......@@ -143,10 +143,12 @@ trait ActiveQueryTrait
}
/**
* @param ActiveRecord[] $models
* @param array $with
* Finds records corresponding to one or multiple relations and populates them into the primary models.
* @param array $with a list of relations that this query should be performed with. Please
* refer to [[with()]] for details about specifying this parameter.
* @param ActiveRecord[] $models the primary models
*/
private function populateRelations(&$models, $with)
public function findWith($with, &$models)
{
$primaryModel = new $this->modelClass;
$relations = $this->normalizeRelations($primaryModel, $with);
......@@ -155,7 +157,7 @@ trait ActiveQueryTrait
// inherit asArray from primary query
$relation->asArray = $this->asArray;
}
$relation->findWith($name, $models);
$relation->populateRelation($name, $models);
}
}
......
......@@ -73,13 +73,12 @@ trait ActiveRelationTrait
/**
* Finds the related records and populates them into the primary models.
* This method is internally used by [[ActiveQuery]]. Do not call it directly.
* @param string $name the relation name
* @param array $primaryModels primary models
* @return array the related models
* @throws InvalidConfigException
* @throws InvalidConfigException if [[link]] is invalid
*/
public function findWith($name, &$primaryModels)
public function populateRelation($name, &$primaryModels)
{
if (!is_array($this->link)) {
throw new InvalidConfigException('Invalid link: it must be an array of key-value pairs.');
......@@ -96,7 +95,7 @@ trait ActiveRelationTrait
/** @var ActiveRelationTrait $viaQuery */
list($viaName, $viaQuery) = $this->via;
$viaQuery->primaryModel = null;
$viaModels = $viaQuery->findWith($viaName, $primaryModels);
$viaModels = $viaQuery->populateRelation($viaName, $primaryModels);
$this->filterByModels($viaModels);
} else {
$this->filterByModels($primaryModels);
......
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