Commit a2fe1284 by Qiang Xue

refactored query and relation.

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