ActiveQuery.php 9.16 KB
Newer Older
Qiang Xue committed
1 2 3 4
<?php
/**
 * @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 8
 * @license http://www.yiiframework.com/license/
 */

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

Qiang Xue committed
11 12 13 14 15 16 17 18 19 20
/**
 * ActiveQuery represents a DB query associated with an Active Record class.
 *
 * ActiveQuery instances are usually created by [[ActiveRecord::find()]], [[ActiveRecord::findBySql()]]
 * and [[ActiveRecord::count()]].
 *
 * ActiveQuery mainly provides the following methods to retrieve the query results:
 *
 * - [[one()]]: returns a single record populated with the first row of data.
 * - [[all()]]: returns all records based on the query results.
21 22 23 24 25 26
 * - [[count()]]: returns the number of records.
 * - [[sum()]]: returns the sum over the specified column.
 * - [[average()]]: returns the average over the specified column.
 * - [[min()]]: returns the min over the specified column.
 * - [[max()]]: returns the max over the specified column.
 * - [[scalar()]]: returns the value of the first column in the first row of the query result.
Qiang Xue committed
27 28 29 30 31 32 33
 * - [[exists()]]: returns a value indicating whether the query result has data or not.
 *
 * Because ActiveQuery extends from [[Query]], one can use query methods, such as [[where()]],
 * [[orderBy()]] to customize the query options.
 *
 * ActiveQuery also provides the following additional query options:
 *
Qiang Xue committed
34 35 36
 * - [[with()]]: list of relations that this query should be performed with.
 * - [[indexBy()]]: the name of the column by which the query result should be indexed.
 * - [[asArray()]]: whether to return each record as an array.
Qiang Xue committed
37 38 39 40 41 42 43 44 45 46
 *
 * These options can be configured using methods of the same name. For example:
 *
 * ~~~
 * $customers = Customer::find()->with('orders')->asArray()->all();
 * ~~~
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
47
class ActiveQuery extends Query
Qiang Xue committed
48 49 50 51 52 53 54 55
{
	/**
	 * @var string the name of the ActiveRecord class.
	 */
	public $modelClass;
	/**
	 * @var array list of relations that this query should be performed with
	 */
Qiang Xue committed
56
	public $with;
Qiang Xue committed
57 58 59 60 61 62 63 64 65 66 67 68
	/**
	 * @var boolean whether to return each record as an array. If false (default), an object
	 * of [[modelClass]] will be created to represent each record.
	 */
	public $asArray;
	/**
	 * @var string the SQL statement to be executed for retrieving AR records.
	 * This is set by [[ActiveRecord::findBySql()]].
	 */
	public $sql;


69 70 71 72 73 74 75 76 77 78 79 80
	/**
	 * PHP magic method.
	 * This method allows calling static method defined in [[modelClass]] via this query object.
	 * It is mainly implemented for supporting the feature of scope.
	 * @param string $name the method name to be called
	 * @param array $params the parameters passed to the method
	 * @return mixed the method return result
	 */
	public function __call($name, $params)
	{
		if (method_exists($this->modelClass, $name)) {
			array_unshift($params, $this);
Alexander Makarov committed
81
			call_user_func_array([$this->modelClass, $name], $params);
Qiang Xue committed
82
			return $this;
83 84 85 86 87
		} else {
			return parent::__call($name, $params);
		}
	}

Qiang Xue committed
88 89
	/**
	 * Executes query and returns all results as an array.
90 91
	 * @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
92 93
	 * @return array the query results. If the query results in nothing, an empty array will be returned.
	 */
94
	public function all($db = null)
Qiang Xue committed
95
	{
96
		$command = $this->createCommand($db);
Qiang Xue committed
97
		$rows = $command->queryAll();
98
		if (!empty($rows)) {
99 100
			$models = $this->createModels($rows);
			if (!empty($this->with)) {
Qiang Xue committed
101
				$this->populateRelations($models, $this->with);
102 103 104
			}
			return $models;
		} else {
Alexander Makarov committed
105
			return [];
Qiang Xue committed
106
		}
Qiang Xue committed
107 108 109 110
	}

	/**
	 * Executes query and returns a single row of result.
111 112
	 * @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
113 114
	 * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
	 * the query result may be either an array or an ActiveRecord object. Null will be returned
Qiang Xue committed
115 116
	 * if the query results in nothing.
	 */
117
	public function one($db = null)
Qiang Xue committed
118
	{
119
		$command = $this->createCommand($db);
120
		$row = $command->queryOne();
121 122 123 124 125 126 127 128
		if ($row !== false) {
			if ($this->asArray) {
				$model = $row;
			} else {
				/** @var $class ActiveRecord */
				$class = $this->modelClass;
				$model = $class::create($row);
			}
Qiang Xue committed
129
			if (!empty($this->with)) {
Alexander Makarov committed
130
				$models = [$model];
Qiang Xue committed
131
				$this->populateRelations($models, $this->with);
Qiang Xue committed
132
				$model = $models[0];
Qiang Xue committed
133 134
			}
			return $model;
135
		} else {
136
			return null;
Qiang Xue committed
137
		}
Qiang Xue committed
138 139 140
	}

	/**
Qiang Xue committed
141
	 * Creates a DB command that can be used to execute this query.
Qiang Xue committed
142 143
	 * @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
144
	 * @return Command the created DB command instance.
Qiang Xue committed
145
	 */
Qiang Xue committed
146
	public function createCommand($db = null)
Qiang Xue committed
147
	{
Qiang Xue committed
148
		/** @var $modelClass ActiveRecord */
Qiang Xue committed
149
		$modelClass = $this->modelClass;
Qiang Xue committed
150
		if ($db === null) {
Qiang Xue committed
151
			$db = $modelClass::getDb();
Qiang Xue committed
152
		}
153 154

		$params = $this->params;
Qiang Xue committed
155 156 157
		if ($this->sql === null) {
			if ($this->from === null) {
				$tableName = $modelClass::tableName();
Qiang Xue committed
158
				if ($this->select === null && !empty($this->join)) {
Alexander Makarov committed
159
					$this->select = ["$tableName.*"];
Qiang Xue committed
160
				}
Alexander Makarov committed
161
				$this->from = [$tableName];
Qiang Xue committed
162
			}
163
			list ($this->sql, $params) = $db->getQueryBuilder()->build($this);
Qiang Xue committed
164
		}
165
		return $db->createCommand($this->sql, $params);
Qiang Xue committed
166 167
	}

Qiang Xue committed
168 169 170
	/**
	 * Sets the [[asArray]] property.
	 * @param boolean $value whether to return the query results in terms of arrays instead of Active Records.
171
	 * @return static the query object itself
Qiang Xue committed
172
	 */
Qiang Xue committed
173 174 175 176 177 178
	public function asArray($value = true)
	{
		$this->asArray = $value;
		return $this;
	}

Qiang Xue committed
179 180 181 182 183 184 185 186 187 188 189 190
	/**
	 * Specifies the relations with which this query should be performed.
	 *
	 * The parameters to this method can be either one or multiple strings, or a single array
	 * of relation names and the optional callbacks to customize the relations.
	 *
	 * The followings are some usage examples:
	 *
	 * ~~~
	 * // find customers together with their orders and country
	 * Customer::find()->with('orders', 'country')->all();
	 * // find customers together with their country and orders of status 1
Alexander Makarov committed
191
	 * Customer::find()->with([
Qiang Xue committed
192 193 194 195
	 *     'orders' => function($query) {
	 *         $query->andWhere('status = 1');
	 *     },
	 *     'country',
Alexander Makarov committed
196
	 * ])->all();
Qiang Xue committed
197 198
	 * ~~~
	 *
199
	 * @return static the query object itself
Qiang Xue committed
200
	 */
Qiang Xue committed
201 202 203 204 205 206 207 208 209 210
	public function with()
	{
		$this->with = func_get_args();
		if (isset($this->with[0]) && is_array($this->with[0])) {
			// the parameter is given as an array
			$this->with = $this->with[0];
		}
		return $this;
	}

Qiang Xue committed
211 212
	/**
	 * Sets the [[indexBy]] property.
213 214 215 216 217 218 219 220 221 222 223 224 225
	 * @param string|callable $column the name of the column by which the query results should be indexed by.
	 * This can also be a callable (e.g. anonymous function) that returns the index value based on the given
	 * row or model data. The signature of the callable should be:
	 *
	 * ~~~
	 * // $model is an AR instance when `asArray` is false,
	 * // or an array of column values when `asArray` is true.
	 * function ($model)
	 * {
	 *     // return the index value corresponding to $model
	 * }
	 * ~~~
	 *
226
	 * @return static the query object itself
Qiang Xue committed
227
	 */
Qiang Xue committed
228
	public function indexBy($column)
Qiang Xue committed
229
	{
Alexander Makarov committed
230
		return parent::indexBy($column);
Qiang Xue committed
231 232
	}

Qiang Xue committed
233
	private function createModels($rows)
Qiang Xue committed
234
	{
Alexander Makarov committed
235
		$models = [];
Qiang Xue committed
236
		if ($this->asArray) {
Qiang Xue committed
237
			if ($this->indexBy === null) {
Qiang Xue committed
238 239 240
				return $rows;
			}
			foreach ($rows as $row) {
241 242 243 244 245 246
				if (is_string($this->indexBy)) {
					$key = $row[$this->indexBy];
				} else {
					$key = call_user_func($this->indexBy, $row);
				}
				$models[$key] = $row;
Qiang Xue committed
247 248 249 250
			}
		} else {
			/** @var $class ActiveRecord */
			$class = $this->modelClass;
Qiang Xue committed
251
			if ($this->indexBy === null) {
Qiang Xue committed
252 253 254 255 256 257
				foreach ($rows as $row) {
					$models[] = $class::create($row);
				}
			} else {
				foreach ($rows as $row) {
					$model = $class::create($row);
258 259 260 261 262 263
					if (is_string($this->indexBy)) {
						$key = $model->{$this->indexBy};
					} else {
						$key = call_user_func($this->indexBy, $model);
					}
					$models[$key] = $model;
Qiang Xue committed
264 265 266 267 268
				}
			}
		}
		return $models;
	}
Qiang Xue committed
269

Qiang Xue committed
270
	private function populateRelations(&$models, $with)
Qiang Xue committed
271 272
	{
		$primaryModel = new $this->modelClass;
Qiang Xue committed
273 274
		$relations = $this->normalizeRelations($primaryModel, $with);
		foreach ($relations as $name => $relation) {
Qiang Xue committed
275 276 277 278
			if ($relation->asArray === null) {
				// inherit asArray from primary query
				$relation->asArray = $this->asArray;
			}
Qiang Xue committed
279
			$relation->findWith($name, $models);
Qiang Xue committed
280 281
		}
	}
Qiang Xue committed
282 283 284 285 286 287

	/**
	 * @param ActiveRecord $model
	 * @param array $with
	 * @return ActiveRelation[]
	 */
Qiang Xue committed
288
	private function normalizeRelations($model, $with)
Qiang Xue committed
289
	{
Alexander Makarov committed
290
		$relations = [];
Qiang Xue committed
291
		foreach ($with as $name => $callback) {
Qiang Xue committed
292
			if (is_integer($name)) {
Qiang Xue committed
293 294
				$name = $callback;
				$callback = null;
Qiang Xue committed
295 296 297 298 299 300 301 302 303
			}
			if (($pos = strpos($name, '.')) !== false) {
				// with sub-relations
				$childName = substr($name, $pos + 1);
				$name = substr($name, 0, $pos);
			} else {
				$childName = null;
			}

Qiang Xue committed
304 305
			$t = strtolower($name);
			if (!isset($relations[$t])) {
306 307 308
				$relation = $model->getRelation($name);
				$relation->primaryModel = null;
				$relations[$t] = $relation;
Qiang Xue committed
309
			} else {
Qiang Xue committed
310
				$relation = $relations[$t];
Qiang Xue committed
311 312 313
			}

			if (isset($childName)) {
Qiang Xue committed
314 315 316
				$relation->with[$childName] = $callback;
			} elseif ($callback !== null) {
				call_user_func($callback, $relation);
Qiang Xue committed
317 318 319 320
			}
		}
		return $relations;
	}
Qiang Xue committed
321
}