ActiveRelation.php 3.21 KB
Newer Older
Qiang Xue committed
1
<?php
Qiang Xue committed
2 3 4 5 6 7 8 9
/**
 * 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/
 */
Qiang Xue committed
10 11 12

namespace yii\db\ar;

Qiang Xue committed
13
/**
Qiang Xue committed
14 15 16 17
 * 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();
Qiang Xue committed
18 19 20 21
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
22
class ActiveRelation extends BaseActiveQuery
Qiang Xue committed
23
{
Qiang Xue committed
24
	/**
Qiang Xue committed
25 26
	 * @var string the class name of the ActiveRecord instances that this relation
	 * should create and populate query results into
Qiang Xue committed
27
	 */
Qiang Xue committed
28
	public $modelClass;
29
	/**
Qiang Xue committed
30 31
	 * @var ActiveRecord the primary record that this relation is associated with.
	 * This is used only in lazy loading with dynamic query options.
32
	 */
Qiang Xue committed
33
	public $primaryModel;
Qiang Xue committed
34
	/**
Qiang Xue committed
35
	 * @var boolean whether this relation is a one-many relation
Qiang Xue committed
36
	 */
Qiang Xue committed
37
	public $hasMany;
Qiang Xue committed
38 39 40
	/**
	 * @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
Qiang Xue committed
41 42
	 * 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.
Qiang Xue committed
43 44
	 */
	public $link;
Qiang Xue committed
45
	/**
Qiang Xue committed
46
	 * @var ActiveRelation
Qiang Xue committed
47
	 */
Qiang Xue committed
48
	public $via;
Qiang Xue committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

	public function get()
	{

	}

	public function findWith($name, &$primaryRecords)
	{
		if (empty($this->link) || !is_array($this->link)) {
			throw new \yii\base\Exception('invalid link');
		}
		$this->addLinkCondition($primaryRecords);
		$records = $this->find();

		/** @var array $map mapping key(s) to index of $primaryRecords */
		$index = $this->buildRecordIndex($primaryRecords, array_values($this->link));
		$this->initRecordRelation($primaryRecords, $name);
		foreach ($records as $record) {
			$key = $this->getRecordKey($record, array_keys($this->link));
			if (isset($index[$key])) {
				$primaryRecords[$map[$key]][$name] = $record;
			}
		}
	}

	protected function getRecordKey($record, $attributes)
	{
		if (isset($attributes[1])) {
			$key = array();
			foreach ($attributes as $attribute) {
				$key[] = is_array($record) ? $record[$attribute] : $record->$attribute;
			}
			return serialize($key);
		} else {
			$attribute = $attributes[0];
			return is_array($record) ? $record[$attribute] : $record->$attribute;
		}
	}

	protected function buildRecordIndex($records, $attributes)
	{
		$map = array();
		foreach ($records as $i => $record) {
			$map[$this->getRecordKey($record, $attributes)] = $i;
		}
		return $map;
	}

	protected function addLinkCondition($primaryRecords)
	{
		$attributes = array_keys($this->link);
		$values = array();
		if (isset($links[1])) {
			// composite keys
			foreach ($primaryRecords as $record) {
				$v = array();
				foreach ($this->link as $attribute => $link) {
					$v[$attribute] = is_array($record) ? $record[$link] : $record->$link;
				}
				$values[] = $v;
			}
		} else {
			// single key
			$attribute = $this->link[$links[0]];
			foreach ($primaryRecords as $record) {
				$values[] = is_array($record) ? $record[$attribute] : $record->$attribute;
			}
		}
		$this->andWhere(array('in', $attributes, $values));
	}

Qiang Xue committed
120
}