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.

201 lines
5.0 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-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
13 years ago
namespace yii\db\ar;
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 ActiveRecord the primary model that this relation is associated with.
12 years ago
* This is used only in lazy loading with dynamic query options.
*/
12 years ago
public $primaryModel;
/**
12 years ago
* @var boolean whether this relation should populate all query results into AR instances.
* If false, only the first row of the results will be taken.
13 years ago
*/
12 years ago
public $multiple;
13 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
*/
public $link;
/**
12 years ago
* @var array
13 years ago
*/
13 years ago
public $via;
12 years ago
/**
* @var array
*/
public $viaTable;
public function via($modelClass, $properties = array())
{
12 years ago
$this->via = $modelClass;
12 years ago
return $this;
}
public function viaTable($tableName, $link, $properties = array())
{
$this->viaTable = array($tableName, $link, $properties);
return $this;
}
12 years ago
public function createCommand()
12 years ago
{
12 years ago
if ($this->primaryModel !== null) {
if ($this->via !== null) {
/** @var $viaQuery ActiveRelation */
$viaName = $this->via;
12 years ago
$viaModels = $this->primaryModel->$viaName;
if ($viaModels === null) {
$viaModels = array();
} elseif (!is_array($viaModels)) {
$viaModels = array($viaModels);
}
$this->filterByModels($viaModels);
} else {
$this->filterByModels(array($this->primaryModel));
}
12 years ago
}
return parent::createCommand();
12 years ago
}
12 years ago
public function findWith($name, &$primaryModels, $viaQuery = null)
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 ($viaQuery !== null) {
$viaModels = $viaQuery->findWith($this->via, $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) {
$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);
}
foreach ($primaryModels as $i => $primaryModel) {
if (isset($viaQuery)) {
$key = $this->getModelKey($primaryModel, array_values($viaQuery->link));
} else {
$key = $this->getModelKey($primaryModel, array_values($this->link));
}
if (isset($buckets[$key])) {
$primaryModels[$i][$name] = $buckets[$key];
} else {
$primaryModels[$i][$name] = $this->multiple ? array() : null;
}
}
return $models;
12 years ago
}
}
protected function buildBuckets($models, $link, $viaModels = null, $viaLink = null)
12 years ago
{
$buckets = array();
foreach ($models as $i => $model) {
$key = $this->getModelKey($model, array_keys($link));
12 years ago
if ($this->index !== null) {
$buckets[$key][$i] = $model;
} else {
$buckets[$key][] = $model;
12 years ago
}
12 years ago
}
if ($viaModels !== null) {
$viaBuckets = array();
foreach ($viaModels as $viaModel) {
$key1 = $this->getModelKey($viaModel, array_keys($viaLink));
$key2 = $this->getModelKey($viaModel, array_values($link));
if (isset($buckets[$key2])) {
foreach ($buckets[$key2] as $i => $bucket) {
if ($this->index !== 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;
}
}
$this->andWhere(array('in', $attributes, $values));
}
13 years ago
}