Yii2 Bootstrap 3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
7.6 KiB

13 years ago
<?php
13 years ago
/**
* ActiveRelation class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
13 years ago
namespace yii\db;
13 years ago
use yii\db\Connection;
use yii\db\Command;
use yii\db\QueryBuilder;
12 years ago
13 years ago
/**
12 years ago
* It is used in three scenarios:
* - eager loading: User::find()->with('posts')->all();
* - lazy loading: $user->posts;
* - lazy loading with query options: $user->posts()->where('status=1')->get();
13 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
12 years ago
class ActiveRelation extends ActiveQuery
13 years ago
{
13 years ago
/**
12 years ago
* @var boolean whether this relation should populate all query results into AR instances.
12 years ago
* If false, only the first row of the results will be retrieved.
13 years ago
*/
12 years ago
public $multiple;
13 years ago
/**
12 years ago
* @var ActiveRecord the primary model that this relation is associated with.
* This is used only in lazy loading with dynamic query options.
*/
12 years ago
public $primaryModel;
12 years ago
/**
13 years ago
* @var array the columns of the primary and foreign tables that establish the relation.
* The array keys must be columns of the table for this relation, and the array values
12 years ago
* must be the corresponding columns from the primary table.
* Do not prefix or quote the column names as they will be done automatically by Yii.
13 years ago
*/
12 years ago
protected $link;
13 years ago
/**
12 years ago
* @var array|ActiveRelation
13 years ago
*/
12 years ago
protected $via;
12 years ago
12 years ago
/**
12 years ago
* @param string $relationName
* @param callback $callback
12 years ago
* @return ActiveRelation
* @throws Exception
12 years ago
*/
public function via($relationName, $callback = null)
12 years ago
{
$getter = 'get' . $relationName;
if (method_exists($this->primaryModel, $getter)) {
$relation = $this->primaryModel->$getter();
if ($relation instanceof ActiveRelation) {
$relation->primaryModel = null;
$this->via = array($relationName, $relation);
if ($callback !== null) {
call_user_func($callback, $relation);
}
return $this;
}
12 years ago
}
throw new Exception('Unknown relation: ' . $relationName);
12 years ago
}
12 years ago
/**
* @param string $tableName
* @param array $link
* @param callback $callback
12 years ago
* @return ActiveRelation
*/
public function viaTable($tableName, $link, $callback = null)
12 years ago
{
12 years ago
$relation = new ActiveRelation(array(
'modelClass' => get_class($this->primaryModel),
'from' => array($tableName),
'link' => $link,
'multiple' => true,
'asArray' => true,
));
$this->via = $relation;
if ($callback !== null) {
call_user_func($callback, $relation);
12 years ago
}
12 years ago
return $this;
}
12 years ago
12 years ago
/**
* Creates a DB command that can be used to execute this query.
12 years ago
* @param Connection $db the DB connection used to create the DB command.
* If null, the DB connection returned by [[modelClass]] will be used.
12 years ago
* @return Command the created DB command instance.
*/
12 years ago
public function createCommand($db = null)
12 years ago
{
12 years ago
if ($this->primaryModel !== null) {
12 years ago
// lazy loading
if ($this->via instanceof self) {
// via pivot table
$viaModels = $this->via->findPivotRows(array($this->primaryModel));
$this->filterByModels($viaModels);
} elseif (is_array($this->via)) {
// via relation
/** @var $viaQuery ActiveRelation */
list($viaName, $viaQuery) = $this->via;
$viaQuery->primaryModel = $this->primaryModel;
if ($viaQuery->multiple) {
$viaModels = $viaQuery->all();
$this->primaryModel->populateRelation($viaName, $viaModels);
} else {
$model = $viaQuery->one();
$this->primaryModel->populateRelation($viaName, $model);
$viaModels = $model === null ? array() : array($model);
12 years ago
}
$this->filterByModels($viaModels);
} else {
$this->filterByModels(array($this->primaryModel));
}
12 years ago
}
12 years ago
return parent::createCommand($db);
12 years ago
}
12 years ago
public function findWith($name, &$primaryModels)
12 years ago
{
12 years ago
if (!is_array($this->link)) {
12 years ago
throw new \yii\base\Exception('invalid link');
}
12 years ago
12 years ago
if ($this->via instanceof self) {
// via pivot table
/** @var $viaQuery ActiveRelation */
$viaQuery = $this->via;
$viaModels = $viaQuery->findPivotRows($primaryModels);
$this->filterByModels($viaModels);
} elseif (is_array($this->via)) {
// via relation
/** @var $viaQuery ActiveRelation */
list($viaName, $viaQuery) = $this->via;
$viaModels = $viaQuery->findWith($viaName, $primaryModels);
$this->filterByModels($viaModels);
} else {
$this->filterByModels($primaryModels);
12 years ago
}
12 years ago
if (count($primaryModels) === 1 && !$this->multiple) {
$model = $this->one();
12 years ago
foreach ($primaryModels as $i => $primaryModel) {
if ($primaryModel instanceof ActiveRecord) {
$primaryModel->populateRelation($name, $model);
} else {
$primaryModels[$i][$name] = $model;
}
12 years ago
}
return array($model);
12 years ago
} else {
$models = $this->all();
if (isset($viaModels, $viaQuery)) {
$buckets = $this->buildBuckets($models, $this->link, $viaModels, $viaQuery->link);
} else {
$buckets = $this->buildBuckets($models, $this->link);
}
12 years ago
$link = array_values(isset($viaQuery) ? $viaQuery->link : $this->link);
foreach ($primaryModels as $i => $primaryModel) {
12 years ago
$key = $this->getModelKey($primaryModel, $link);
$value = isset($buckets[$key]) ? $buckets[$key] : ($this->multiple ? array() : null);
if ($primaryModel instanceof ActiveRecord) {
$primaryModel->populateRelation($name, $value);
} else {
$primaryModels[$i][$name] = $value;
}
}
return $models;
12 years ago
}
}
protected function buildBuckets($models, $link, $viaModels = null, $viaLink = null)
12 years ago
{
$buckets = array();
12 years ago
$linkKeys = array_keys($link);
12 years ago
foreach ($models as $i => $model) {
12 years ago
$key = $this->getModelKey($model, $linkKeys);
12 years ago
if ($this->indexBy !== null) {
12 years ago
$buckets[$key][$i] = $model;
} else {
$buckets[$key][] = $model;
12 years ago
}
12 years ago
}
if ($viaModels !== null) {
$viaBuckets = array();
12 years ago
$viaLinkKeys = array_keys($viaLink);
$linkValues = array_values($link);
foreach ($viaModels as $viaModel) {
12 years ago
$key1 = $this->getModelKey($viaModel, $viaLinkKeys);
$key2 = $this->getModelKey($viaModel, $linkValues);
if (isset($buckets[$key2])) {
foreach ($buckets[$key2] as $i => $bucket) {
12 years ago
if ($this->indexBy !== null) {
$viaBuckets[$key1][$i] = $bucket;
} else {
$viaBuckets[$key1][] = $bucket;
}
}
}
}
$buckets = $viaBuckets;
}
12 years ago
if (!$this->multiple) {
foreach ($buckets as $i => $bucket) {
$buckets[$i] = reset($bucket);
12 years ago
}
12 years ago
}
return $buckets;
12 years ago
}
12 years ago
protected function getModelKey($model, $attributes)
12 years ago
{
12 years ago
if (count($attributes) > 1) {
12 years ago
$key = array();
foreach ($attributes as $attribute) {
12 years ago
$key[] = $model[$attribute];
12 years ago
}
return serialize($key);
} else {
12 years ago
$attribute = reset($attributes);
return $model[$attribute];
12 years ago
}
}
protected function filterByModels($models)
12 years ago
{
$attributes = array_keys($this->link);
$values = array();
12 years ago
if (count($attributes) ===1) {
// single key
$attribute = reset($this->link);
foreach ($models as $model) {
$values[] = $model[$attribute];
}
} else {
12 years ago
// composite keys
foreach ($models as $model) {
12 years ago
$v = array();
foreach ($this->link as $attribute => $link) {
12 years ago
$v[$attribute] = $model[$link];
12 years ago
}
$values[] = $v;
}
}
12 years ago
$this->andWhere(array('in', $attributes, array_unique($values, SORT_REGULAR)));
12 years ago
}
12 years ago
/**
* @param ActiveRecord[] $primaryModels
* @return array
*/
protected function findPivotRows($primaryModels)
{
if (empty($primaryModels)) {
return array();
}
$this->filterByModels($primaryModels);
/** @var $primaryModel ActiveRecord */
$primaryModel = reset($primaryModels);
$db = $primaryModel->getDbConnection();
$sql = $db->getQueryBuilder()->build($this);
return $db->createCommand($sql, $this->params)->queryAll();
}
13 years ago
}