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.
		
		
		
		
			
				
					305 lines
				
				9.2 KiB
			
		
		
			
		
	
	
					305 lines
				
				9.2 KiB
			| 
											14 years ago
										 | <?php
 | ||
| 
											14 years ago
										 | /**
 | ||
|  |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
| 
											13 years ago
										 |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
| 
											14 years ago
										 |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											13 years ago
										 | namespace yii\db;
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											13 years ago
										 | use yii\db\Connection;
 | ||
|  | use yii\db\Command;
 | ||
| 
											13 years ago
										 | use yii\base\InvalidConfigException;
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											14 years ago
										 | /**
 | ||
| 
											13 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.
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
| 
											13 years ago
										 | class ActiveRelation extends ActiveQuery
 | ||
| 
											14 years ago
										 | {
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var boolean whether this relation should populate all query results into AR instances.
 | ||
| 
											13 years ago
										 | 	 * If false, only the first row of the results will be retrieved.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $multiple;
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var ActiveRecord the primary model that this relation is associated with.
 | ||
|  | 	 * This is used only in lazy loading with dynamic query options.
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public $primaryModel;
 | ||
| 
											13 years ago
										 | 	/**
 | ||
| 
											14 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
 | ||
| 
											13 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.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $link;
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var array|ActiveRelation the query associated with the pivot table. Please call [[via()]]
 | ||
|  | 	 * or [[viaTable()]] to set this property instead of directly setting it.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $via;
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * Specifies the relation associated with the pivot table.
 | ||
|  | 	 * @param string $relationName the relation name. This refers to a relation declared in [[primaryModel]].
 | ||
| 
											13 years ago
										 | 	 * @param callable $callable a PHP callback for customizing the relation associated with the pivot table.
 | ||
| 
											13 years ago
										 | 	 * Its signature should be `function($query)`, where `$query` is the query to be customized.
 | ||
|  | 	 * @return ActiveRelation the relation object itself.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function via($relationName, $callable = null)
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		$relation = $this->primaryModel->getRelation($relationName);
 | ||
|  | 		$this->via = array($relationName, $relation);
 | ||
| 
											13 years ago
										 | 		if ($callable !== null) {
 | ||
|  | 			call_user_func($callable, $relation);
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 		return $this;
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 	/**
 | ||
| 
											13 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.
 | ||
| 
											13 years ago
										 | 	 * @param callable $callable a PHP callback for customizing the relation associated with the pivot table.
 | ||
| 
											13 years ago
										 | 	 * Its signature should be `function($query)`, where `$query` is the query to be customized.
 | ||
| 
											13 years ago
										 | 	 * @return ActiveRelation
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function viaTable($tableName, $link, $callable = null)
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		$relation = new ActiveRelation(array(
 | ||
|  | 			'modelClass' => get_class($this->primaryModel),
 | ||
|  | 			'from' => array($tableName),
 | ||
|  | 			'link' => $link,
 | ||
|  | 			'multiple' => true,
 | ||
|  | 			'asArray' => true,
 | ||
|  | 		));
 | ||
|  | 		$this->via = $relation;
 | ||
| 
											13 years ago
										 | 		if ($callable !== null) {
 | ||
|  | 			call_user_func($callable, $relation);
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 		return $this;
 | ||
|  | 	}
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * Creates a DB command that can be used to execute this query.
 | ||
| 
											13 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.
 | ||
| 
											13 years ago
										 | 	 * @return Command the created DB command instance.
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function createCommand($db = null)
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		if ($this->primaryModel !== null) {
 | ||
| 
											13 years ago
										 | 			// lazy loading
 | ||
|  | 			if ($this->via instanceof self) {
 | ||
|  | 				// via pivot table
 | ||
|  | 				$viaModels = $this->via->findPivotRows(array($this->primaryModel));
 | ||
|  | 				$this->filterByModels($viaModels);
 | ||
|  | 			} elseif (is_array($this->via)) {
 | ||
|  | 				// via relation
 | ||
| 
											13 years ago
										 | 				/** @var $viaQuery ActiveRelation */
 | ||
|  | 				list($viaName, $viaQuery) = $this->via;
 | ||
|  | 				if ($viaQuery->multiple) {
 | ||
| 
											13 years ago
										 | 					$viaModels = $viaQuery->all();
 | ||
|  | 					$this->primaryModel->populateRelation($viaName, $viaModels);
 | ||
| 
											13 years ago
										 | 				} else {
 | ||
| 
											13 years ago
										 | 					$model = $viaQuery->one();
 | ||
|  | 					$this->primaryModel->populateRelation($viaName, $model);
 | ||
| 
											13 years ago
										 | 					$viaModels = $model === null ? array() : array($model);
 | ||
| 
											13 years ago
										 | 				}
 | ||
| 
											13 years ago
										 | 				$this->filterByModels($viaModels);
 | ||
|  | 			} else {
 | ||
|  | 				$this->filterByModels(array($this->primaryModel));
 | ||
|  | 			}
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 		return parent::createCommand($db);
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * Finds the related records and populates them into the primary models.
 | ||
| 
											13 years ago
										 | 	 * This method is internally used by [[ActiveQuery]]. Do not call it directly.
 | ||
| 
											13 years ago
										 | 	 * @param string $name the relation name
 | ||
|  | 	 * @param array $primaryModels primary models
 | ||
|  | 	 * @return array the related models
 | ||
| 
											13 years ago
										 | 	 * @throws InvalidConfigException
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function findWith($name, &$primaryModels)
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		if (!is_array($this->link)) {
 | ||
| 
											13 years ago
										 | 			throw new InvalidConfigException('Invalid link: it must be an array of key-value pairs.');
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 		if ($this->via instanceof self) {
 | ||
|  | 			// via pivot table
 | ||
|  | 			/** @var $viaQuery ActiveRelation */
 | ||
|  | 			$viaQuery = $this->via;
 | ||
|  | 			$viaModels = $viaQuery->findPivotRows($primaryModels);
 | ||
|  | 			$this->filterByModels($viaModels);
 | ||
|  | 		} elseif (is_array($this->via)) {
 | ||
|  | 			// via relation
 | ||
|  | 			/** @var $viaQuery ActiveRelation */
 | ||
|  | 			list($viaName, $viaQuery) = $this->via;
 | ||
| 
											13 years ago
										 | 			$viaQuery->primaryModel = null;
 | ||
| 
											13 years ago
										 | 			$viaModels = $viaQuery->findWith($viaName, $primaryModels);
 | ||
| 
											13 years ago
										 | 			$this->filterByModels($viaModels);
 | ||
|  | 		} else {
 | ||
|  | 			$this->filterByModels($primaryModels);
 | ||
| 
											13 years ago
										 | 		}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 		if (count($primaryModels) === 1 && !$this->multiple) {
 | ||
| 
											13 years ago
										 | 			$model = $this->one();
 | ||
| 
											13 years ago
										 | 			foreach ($primaryModels as $i => $primaryModel) {
 | ||
| 
											13 years ago
										 | 				if ($primaryModel instanceof ActiveRecord) {
 | ||
|  | 					$primaryModel->populateRelation($name, $model);
 | ||
|  | 				} else {
 | ||
|  | 					$primaryModels[$i][$name] = $model;
 | ||
|  | 				}
 | ||
| 
											13 years ago
										 | 			}
 | ||
| 
											13 years ago
										 | 			return array($model);
 | ||
| 
											13 years ago
										 | 		} else {
 | ||
|  | 			$models = $this->all();
 | ||
| 
											13 years ago
										 | 			if (isset($viaModels, $viaQuery)) {
 | ||
|  | 				$buckets = $this->buildBuckets($models, $this->link, $viaModels, $viaQuery->link);
 | ||
|  | 			} else {
 | ||
|  | 				$buckets = $this->buildBuckets($models, $this->link);
 | ||
|  | 			}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 			$link = array_values(isset($viaQuery) ? $viaQuery->link : $this->link);
 | ||
| 
											13 years ago
										 | 			foreach ($primaryModels as $i => $primaryModel) {
 | ||
| 
											13 years ago
										 | 				$key = $this->getModelKey($primaryModel, $link);
 | ||
| 
											13 years ago
										 | 				$value = isset($buckets[$key]) ? $buckets[$key] : ($this->multiple ? array() : null);
 | ||
|  | 				if ($primaryModel instanceof ActiveRecord) {
 | ||
|  | 					$primaryModel->populateRelation($name, $value);
 | ||
| 
											13 years ago
										 | 				} else {
 | ||
| 
											13 years ago
										 | 					$primaryModels[$i][$name] = $value;
 | ||
| 
											13 years ago
										 | 				}
 | ||
|  | 			}
 | ||
|  | 			return $models;
 | ||
| 
											13 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * @param array $models
 | ||
|  | 	 * @param array $link
 | ||
|  | 	 * @param array $viaModels
 | ||
|  | 	 * @param array $viaLink
 | ||
|  | 	 * @return array
 | ||
|  | 	 */
 | ||
|  | 	private function buildBuckets($models, $link, $viaModels = null, $viaLink = null)
 | ||
| 
											13 years ago
										 | 	{
 | ||
|  | 		$buckets = array();
 | ||
| 
											13 years ago
										 | 		$linkKeys = array_keys($link);
 | ||
| 
											13 years ago
										 | 		foreach ($models as $i => $model) {
 | ||
| 
											13 years ago
										 | 			$key = $this->getModelKey($model, $linkKeys);
 | ||
| 
											13 years ago
										 | 			if ($this->indexBy !== null) {
 | ||
| 
											13 years ago
										 | 				$buckets[$key][$i] = $model;
 | ||
|  | 			} else {
 | ||
|  | 				$buckets[$key][] = $model;
 | ||
| 
											13 years ago
										 | 			}
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 		if ($viaModels !== null) {
 | ||
|  | 			$viaBuckets = array();
 | ||
| 
											13 years ago
										 | 			$viaLinkKeys = array_keys($viaLink);
 | ||
|  | 			$linkValues = array_values($link);
 | ||
| 
											13 years ago
										 | 			foreach ($viaModels as $viaModel) {
 | ||
| 
											13 years ago
										 | 				$key1 = $this->getModelKey($viaModel, $viaLinkKeys);
 | ||
|  | 				$key2 = $this->getModelKey($viaModel, $linkValues);
 | ||
| 
											13 years ago
										 | 				if (isset($buckets[$key2])) {
 | ||
|  | 					foreach ($buckets[$key2] as $i => $bucket) {
 | ||
| 
											13 years ago
										 | 						if ($this->indexBy !== null) {
 | ||
| 
											13 years ago
										 | 							$viaBuckets[$key1][$i] = $bucket;
 | ||
|  | 						} else {
 | ||
|  | 							$viaBuckets[$key1][] = $bucket;
 | ||
|  | 						}
 | ||
|  | 					}
 | ||
|  | 				}
 | ||
|  | 			}
 | ||
|  | 			$buckets = $viaBuckets;
 | ||
|  | 		}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 		if (!$this->multiple) {
 | ||
|  | 			foreach ($buckets as $i => $bucket) {
 | ||
|  | 				$buckets[$i] = reset($bucket);
 | ||
| 
											13 years ago
										 | 			}
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 		return $buckets;
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * @param ActiveRecord|array $model
 | ||
|  | 	 * @param array $attributes
 | ||
|  | 	 * @return string
 | ||
|  | 	 */
 | ||
|  | 	private function getModelKey($model, $attributes)
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		if (count($attributes) > 1) {
 | ||
| 
											13 years ago
										 | 			$key = array();
 | ||
|  | 			foreach ($attributes as $attribute) {
 | ||
| 
											13 years ago
										 | 				$key[] = $model[$attribute];
 | ||
| 
											13 years ago
										 | 			}
 | ||
|  | 			return serialize($key);
 | ||
|  | 		} else {
 | ||
| 
											13 years ago
										 | 			$attribute = reset($attributes);
 | ||
|  | 			return $model[$attribute];
 | ||
| 
											13 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * @param array $models
 | ||
|  | 	 */
 | ||
|  | 	private function filterByModels($models)
 | ||
| 
											13 years ago
										 | 	{
 | ||
|  | 		$attributes = array_keys($this->link);
 | ||
|  | 		$values = array();
 | ||
| 
											13 years ago
										 | 		if (count($attributes) === 1) {
 | ||
| 
											13 years ago
										 | 			// single key
 | ||
|  | 			$attribute = reset($this->link);
 | ||
|  | 			foreach ($models as $model) {
 | ||
|  | 				$values[] = $model[$attribute];
 | ||
|  | 			}
 | ||
|  | 		} else {
 | ||
| 
											13 years ago
										 | 			// composite keys
 | ||
| 
											13 years ago
										 | 			foreach ($models as $model) {
 | ||
| 
											13 years ago
										 | 				$v = array();
 | ||
|  | 				foreach ($this->link as $attribute => $link) {
 | ||
| 
											13 years ago
										 | 					$v[$attribute] = $model[$link];
 | ||
| 
											13 years ago
										 | 				}
 | ||
|  | 				$values[] = $v;
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
| 
											13 years ago
										 | 		$this->andWhere(array('in', $attributes, array_unique($values, SORT_REGULAR)));
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * @param ActiveRecord[] $primaryModels
 | ||
|  | 	 * @return array
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	private function findPivotRows($primaryModels)
 | ||
| 
											13 years ago
										 | 	{
 | ||
|  | 		if (empty($primaryModels)) {
 | ||
|  | 			return array();
 | ||
|  | 		}
 | ||
|  | 		$this->filterByModels($primaryModels);
 | ||
|  | 		/** @var $primaryModel ActiveRecord */
 | ||
|  | 		$primaryModel = reset($primaryModels);
 | ||
| 
											13 years ago
										 | 		$db = $primaryModel->getDb();
 | ||
| 
											13 years ago
										 | 		$sql = $db->getQueryBuilder()->build($this);
 | ||
|  | 		return $db->createCommand($sql, $this->params)->queryAll();
 | ||
|  | 	}
 | ||
| 
											14 years ago
										 | }
 |