ActiveRelation.php 3.06 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
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
24
 * @author Carsten Brandt <mail@cebe.cc>
Qiang Xue committed
25 26
 * @since 2.0
 */
27
class ActiveRelation extends ActiveQuery implements ActiveRelationInterface
Qiang Xue committed
28
{
29
	use ActiveRelationTrait;
Qiang Xue committed
30 31

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

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