Skip to content

Commit

Permalink
refactored query and relation.
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed Nov 17, 2013
1 parent 684ee63 commit a2fe128
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
4 changes: 2 additions & 2 deletions framework/yii/db/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function all($db = null)
if (!empty($rows)) {
$models = $this->createModels($rows);
if (!empty($this->with)) {
$this->populateRelations($models, $this->with);
$this->findWith($this->with, $models);
}
return $models;
} else {
Expand Down Expand Up @@ -98,7 +98,7 @@ public function one($db = null)
}
if (!empty($this->with)) {
$models = [$model];
$this->populateRelations($models, $this->with);
$this->findWith($this->with, $models);
$model = $models[0];
}
return $model;
Expand Down
12 changes: 7 additions & 5 deletions framework/yii/db/ActiveQueryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/**
Expand Down Expand Up @@ -143,10 +143,12 @@ private function createModels($rows)
}

/**
* @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)

This comment has been minimized.

Copy link
@cebe

cebe Nov 17, 2013

Member

should it become part of the interface then?

This comment has been minimized.

Copy link
@qiangxue

qiangxue Nov 17, 2013

Author Member

I'm really not sure. Similar question is with ActiveRelation::populateRelation().

This comment has been minimized.

Copy link
@cebe

cebe Nov 17, 2013

Member

Okay, lets review interfaces later then.

{
$primaryModel = new $this->modelClass;
$relations = $this->normalizeRelations($primaryModel, $with);
Expand All @@ -155,7 +157,7 @@ private function populateRelations(&$models, $with)
// inherit asArray from primary query
$relation->asArray = $this->asArray;
}
$relation->findWith($name, $models);
$relation->populateRelation($name, $models);
}
}

Expand Down
7 changes: 3 additions & 4 deletions framework/yii/db/ActiveRelationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,12 @@ public function via($relationName, $callable = null)

/**
* 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.');
Expand All @@ -96,7 +95,7 @@ public function findWith($name, &$primaryModels)
/** @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);
Expand Down

0 comments on commit a2fe128

Please sign in to comment.