ActiveQuery.php 4.57 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
/**
 * ActiveQuery represents a DB query associated with an Active Record class.
 *
14
 * ActiveQuery instances are usually created by [[ActiveRecord::find()]] and [[ActiveRecord::findBySql()]].
Qiang Xue committed
15 16 17 18 19
 *
 * 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.
20 21 22 23 24 25
 * - [[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.
26
 * - [[column()]]: returns the value of the first column in 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
 *
 * 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>
45
 * @author Carsten Brandt <mail@cebe.cc>
Qiang Xue committed
46 47
 * @since 2.0
 */
48
class ActiveQuery extends Query implements ActiveQueryInterface
Qiang Xue committed
49
{
50
	use ActiveQueryTrait;
51

Qiang Xue committed
52 53 54 55 56 57 58 59 60
	/**
	 * @var string the SQL statement to be executed for retrieving AR records.
	 * This is set by [[ActiveRecord::findBySql()]].
	 */
	public $sql;


	/**
	 * Executes query and returns all results as an array.
61 62
	 * @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
63 64
	 * @return array the query results. If the query results in nothing, an empty array will be returned.
	 */
65
	public function all($db = null)
Qiang Xue committed
66
	{
67
		$command = $this->createCommand($db);
Qiang Xue committed
68
		$rows = $command->queryAll();
69
		if (!empty($rows)) {
70 71
			$models = $this->createModels($rows);
			if (!empty($this->with)) {
72
				$this->findWith($this->with, $models);
73 74 75
			}
			return $models;
		} else {
Alexander Makarov committed
76
			return [];
Qiang Xue committed
77
		}
Qiang Xue committed
78 79 80 81
	}

	/**
	 * Executes query and returns a single row of result.
82 83
	 * @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
84 85
	 * @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
86 87
	 * if the query results in nothing.
	 */
88
	public function one($db = null)
Qiang Xue committed
89
	{
90
		$command = $this->createCommand($db);
91
		$row = $command->queryOne();
92 93 94 95
		if ($row !== false) {
			if ($this->asArray) {
				$model = $row;
			} else {
slavcodev committed
96
				/** @var ActiveRecord $class */
97 98 99
				$class = $this->modelClass;
				$model = $class::create($row);
			}
Qiang Xue committed
100
			if (!empty($this->with)) {
Alexander Makarov committed
101
				$models = [$model];
102
				$this->findWith($this->with, $models);
Qiang Xue committed
103
				$model = $models[0];
Qiang Xue committed
104 105
			}
			return $model;
106
		} else {
107
			return null;
Qiang Xue committed
108
		}
Qiang Xue committed
109 110 111
	}

	/**
Qiang Xue committed
112
	 * Creates a DB command that can be used to execute this query.
Qiang Xue committed
113 114
	 * @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
115
	 * @return Command the created DB command instance.
Qiang Xue committed
116
	 */
Qiang Xue committed
117
	public function createCommand($db = null)
Qiang Xue committed
118
	{
slavcodev committed
119
		/** @var ActiveRecord $modelClass */
Qiang Xue committed
120
		$modelClass = $this->modelClass;
Qiang Xue committed
121
		if ($db === null) {
Qiang Xue committed
122
			$db = $modelClass::getDb();
Qiang Xue committed
123
		}
124

Qiang Xue committed
125
		if ($this->sql === null) {
Carsten Brandt committed
126 127 128
			$select = $this->select;
			$from = $this->from;

Qiang Xue committed
129 130
			if ($this->from === null) {
				$tableName = $modelClass::tableName();
Qiang Xue committed
131
				if ($this->select === null && !empty($this->join)) {
Alexander Makarov committed
132
					$this->select = ["$tableName.*"];
Qiang Xue committed
133
				}
Alexander Makarov committed
134
				$this->from = [$tableName];
Qiang Xue committed
135
			}
Carsten Brandt committed
136 137 138 139 140 141 142
			list ($sql, $params) = $db->getQueryBuilder()->build($this);

			$this->select = $select;
			$this->from = $from;
		} else {
			$sql = $this->sql;
			$params = $this->params;
Qiang Xue committed
143
		}
Carsten Brandt committed
144
		return $db->createCommand($sql, $params);
Qiang Xue committed
145 146
	}
}