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.

93 lines
3.2 KiB

13 years ago
<?php
13 years ago
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
13 years ago
namespace yii\db;
13 years ago
13 years ago
/**
12 years ago
* ActiveRelation represents a relation between two Active Record classes.
*
* ActiveRelation instances are usually created by calling [[ActiveRecord::hasOne()]] and
* [[ActiveRecord::hasMany()]]. An Active Record class declares a relation by defining
* a getter method which calls one of the above methods and returns the created ActiveRelation object.
*
* A relation is specified by [[link]] which represents the association between columns
* of different tables; and the multiplicity of the relation is indicated by [[multiple]].
*
* If a relation involves a pivot table, it may be specified by [[via()]] or [[viaTable()]] method.
13 years ago
*
* @property array|ActiveRelation $via the query associated with the pivot table. Please call [[via()]]
* or [[viaTable()]] to set this property instead of directly setting it.
*
13 years ago
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
12 years ago
class ActiveRelation extends ActiveQuery
13 years ago
{
use \yii\ar\ActiveRelation;
13 years ago
/**
12 years ago
* Specifies the pivot table.
* @param string $tableName the name of the pivot table.
* @param array $link the link between the pivot table and the table associated with [[primaryModel]].
* The keys of the array represent the columns in the pivot table, and the values represent the columns
* in the [[primaryModel]] table.
* @param callable $callable a PHP callback for customizing the relation associated with the pivot table.
12 years ago
* Its signature should be `function($query)`, where `$query` is the query to be customized.
* @return static
12 years ago
*/
public function viaTable($tableName, $link, $callable = null)
12 years ago
{
$relation = new ActiveRelation([
12 years ago
'modelClass' => get_class($this->primaryModel),
'from' => [$tableName],
12 years ago
'link' => $link,
'multiple' => true,
'asArray' => true,
]);
12 years ago
$this->via = $relation;
if ($callable !== null) {
call_user_func($callable, $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([$this->primaryModel]);
12 years ago
$this->filterByModels($viaModels);
} elseif (is_array($this->via)) {
// via relation
/** @var $viaQuery ActiveRelation */
list($viaName, $viaQuery) = $this->via;
if ($viaQuery->multiple) {
$viaModels = $viaQuery->all();
$this->primaryModel->populateRelation($viaName, $viaModels);
} else {
$model = $viaQuery->one();
$this->primaryModel->populateRelation($viaName, $model);
$viaModels = $model === null ? [] : [$model];
12 years ago
}
$this->filterByModels($viaModels);
} else {
$this->filterByModels([$this->primaryModel]);
}
12 years ago
}
12 years ago
return parent::createCommand($db);
12 years ago
}
13 years ago
}