From a2fe1284551c5823e29559bce24504ec21ec0f29 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sun, 17 Nov 2013 00:02:27 -0500 Subject: [PATCH] refactored query and relation. --- framework/yii/db/ActiveQuery.php | 4 ++-- framework/yii/db/ActiveQueryTrait.php | 12 +++++++----- framework/yii/db/ActiveRelationTrait.php | 7 +++---- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/framework/yii/db/ActiveQuery.php b/framework/yii/db/ActiveQuery.php index 4d21fbe..517bf22 100644 --- a/framework/yii/db/ActiveQuery.php +++ b/framework/yii/db/ActiveQuery.php @@ -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; diff --git a/framework/yii/db/ActiveQueryTrait.php b/framework/yii/db/ActiveQueryTrait.php index 51135d8..7aae6e6 100644 --- a/framework/yii/db/ActiveQueryTrait.php +++ b/framework/yii/db/ActiveQueryTrait.php @@ -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); } } diff --git a/framework/yii/db/ActiveRelationTrait.php b/framework/yii/db/ActiveRelationTrait.php index 27963d0..be42eb6 100644 --- a/framework/yii/db/ActiveRelationTrait.php +++ b/framework/yii/db/ActiveRelationTrait.php @@ -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);