ActiveRelation.php 3.24 KB
Newer Older
Qiang Xue committed
1
<?php
Qiang Xue committed
2 3 4
/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.yiiframework.com/
Qiang Xue committed
5
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
6 7
 * @license http://www.yiiframework.com/license/
 */
Qiang Xue committed
8

Qiang Xue committed
9
namespace yii\db;
Qiang Xue committed
10

Qiang Xue committed
11
/**
Qiang Xue committed
12 13 14 15 16 17 18 19 20 21
 * 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.
Qiang Xue committed
22
 *
23 24 25
 * @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.
 *
Qiang Xue committed
26
 * @author Qiang Xue <qiang.xue@gmail.com>
27
 * @author Carsten Brandt <mail@cebe.cc>
Qiang Xue committed
28 29
 * @since 2.0
 */
30
class ActiveRelation extends ActiveQuery implements ActiveRelationInterface
Qiang Xue committed
31
{
32
	use ActiveRelationTrait;
Qiang Xue committed
33 34

	/**
Qiang Xue committed
35 36 37 38 39
	 * 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.
Qiang Xue committed
40
	 * @param callable $callable a PHP callback for customizing the relation associated with the pivot table.
Qiang Xue committed
41
	 * Its signature should be `function($query)`, where `$query` is the query to be customized.
42
	 * @return static
Qiang Xue committed
43
	 */
Qiang Xue committed
44
	public function viaTable($tableName, $link, $callable = null)
Qiang Xue committed
45
	{
Alexander Makarov committed
46
		$relation = new ActiveRelation([
Qiang Xue committed
47
			'modelClass' => get_class($this->primaryModel),
Alexander Makarov committed
48
			'from' => [$tableName],
Qiang Xue committed
49 50 51
			'link' => $link,
			'multiple' => true,
			'asArray' => true,
Alexander Makarov committed
52
		]);
Qiang Xue committed
53
		$this->via = $relation;
Qiang Xue committed
54 55
		if ($callable !== null) {
			call_user_func($callable, $relation);
Qiang Xue committed
56
		}
Qiang Xue committed
57 58
		return $this;
	}
Qiang Xue committed
59

Qiang Xue committed
60 61
	/**
	 * Creates a DB command that can be used to execute this query.
Qiang Xue committed
62 63
	 * @param Connection $db the DB connection used to create the DB command.
	 * If null, the DB connection returned by [[modelClass]] will be used.
Qiang Xue committed
64 65
	 * @return Command the created DB command instance.
	 */
Qiang Xue committed
66
	public function createCommand($db = null)
Qiang Xue committed
67
	{
Qiang Xue committed
68
		if ($this->primaryModel !== null) {
Qiang Xue committed
69 70 71
			// lazy loading
			if ($this->via instanceof self) {
				// via pivot table
Alexander Makarov committed
72
				$viaModels = $this->via->findPivotRows([$this->primaryModel]);
Qiang Xue committed
73 74 75
				$this->filterByModels($viaModels);
			} elseif (is_array($this->via)) {
				// via relation
slavcodev committed
76
				/** @var ActiveRelation $viaQuery */
Qiang Xue committed
77 78
				list($viaName, $viaQuery) = $this->via;
				if ($viaQuery->multiple) {
Qiang Xue committed
79 80
					$viaModels = $viaQuery->all();
					$this->primaryModel->populateRelation($viaName, $viaModels);
Qiang Xue committed
81
				} else {
Qiang Xue committed
82 83
					$model = $viaQuery->one();
					$this->primaryModel->populateRelation($viaName, $model);
Alexander Makarov committed
84
					$viaModels = $model === null ? [] : [$model];
Qiang Xue committed
85
				}
86 87
				$this->filterByModels($viaModels);
			} else {
Alexander Makarov committed
88
				$this->filterByModels([$this->primaryModel]);
89
			}
Qiang Xue committed
90
		}
Qiang Xue committed
91
		return parent::createCommand($db);
Qiang Xue committed
92
	}
Qiang Xue committed
93
}