ActiveQuery.php 5.82 KB
Newer Older
1 2 3 4 5 6 7 8
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\elasticsearch;
Carsten Brandt committed
9

10 11
use yii\db\ActiveQueryInterface;
use yii\db\ActiveQueryTrait;
12 13

/**
14
 * ActiveQuery represents a [[Query]] associated with an [[ActiveRecord]] class.
15
 *
16
 * ActiveQuery instances are usually created by [[ActiveRecord::find()]].
17 18 19 20 21 22 23
 *
 * 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.
 * - [[count()]]: returns the number of records.
 * - [[scalar()]]: returns the value of the first column in the first row of the query result.
24
 * - [[column()]]: returns the value of the first column in the query result.
25 26
 * - [[exists()]]: returns a value indicating whether the query result has data or not.
 *
27 28
 * Because ActiveQuery extends from [[Query]], one can use query methods, such as [[where()]],
 * [[orderBy()]] to customize the query options.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 *
 * ActiveQuery also provides the following additional query options:
 *
 * - [[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.
 *
 * These options can be configured using methods of the same name. For example:
 *
 * ~~~
 * $customers = Customer::find()->with('orders')->asArray()->all();
 * ~~~
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
45
class ActiveQuery extends Query implements ActiveQueryInterface
46
{
47
	use ActiveQueryTrait;
48 49

	/**
50 51 52 53
	 * Creates a DB command that can be used to execute this query.
	 * @param Connection $db the DB connection used to create the DB command.
	 * If null, the DB connection returned by [[modelClass]] will be used.
	 * @return Command the created DB command instance.
54
	 */
55
	public function createCommand($db = null)
56
	{
57
		/** @var ActiveRecord $modelClass */
58 59 60
		$modelClass = $this->modelClass;
		if ($db === null) {
			$db = $modelClass::getDb();
61 62
		}

63 64 65 66 67 68
		if ($this->type === null) {
			$this->type = $modelClass::type();
		}
		if ($this->index === null) {
			$this->index = $modelClass::index();
			$this->type = $modelClass::type();
69
		}
70 71
		$commandConfig = $db->getQueryBuilder()->build($this);
		return $db->createCommand($commandConfig);
72 73 74
	}

	/**
75 76 77 78
	 * Executes query and returns all results as an array.
	 * @param Connection $db the DB connection used to create the DB command.
	 * If null, the DB connection returned by [[modelClass]] will be used.
	 * @return array the query results. If the query results in nothing, an empty array will be returned.
79
	 */
80
	public function all($db = null)
81
	{
82 83
		$result = $this->createCommand($db)->search();
		if (empty($result['hits']['hits'])) {
84 85
			return [];
		}
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
		if ($this->fields !== null) {
			foreach ($result['hits']['hits'] as &$row) {
				$row['_source'] = isset($row['fields']) ? $row['fields'] : [];
				unset($row['fields']);
			}
			unset($row);
		}
		if ($this->asArray && $this->indexBy) {
			foreach ($result['hits']['hits'] as &$row) {
				$row['_source'][ActiveRecord::PRIMARY_KEY_NAME] = $row['_id'];
				$row = $row['_source'];
			}
		}
		$models = $this->createModels($result['hits']['hits']);
		if ($this->asArray && !$this->indexBy) {
101
			foreach($models as $key => $model) {
102
				$model['_source'][ActiveRecord::PRIMARY_KEY_NAME] = $model['_id'];
103 104 105
				$models[$key] = $model['_source'];
			}
		}
106 107
		if (!empty($this->with)) {
			$this->findWith($this->with, $models);
108
		}
109
		return $models;
110 111 112
	}

	/**
113 114 115 116 117 118
	 * Executes query and returns a single row of result.
	 * @param Connection $db the DB connection used to create the DB command.
	 * If null, the DB connection returned by [[modelClass]] will be used.
	 * @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
	 * if the query results in nothing.
119
	 */
120
	public function one($db = null)
121
	{
Carsten Brandt committed
122
		if (($result = parent::one($db)) === false) {
123 124
			return null;
		}
125
		if ($this->asArray) {
Carsten Brandt committed
126
			$model = $result['_source'];
127
			$model[ActiveRecord::PRIMARY_KEY_NAME] = $result['_id'];
128 129 130
		} else {
			/** @var ActiveRecord $class */
			$class = $this->modelClass;
Carsten Brandt committed
131
			$model = $class::create($result);
132 133 134 135 136 137 138
		}
		if (!empty($this->with)) {
			$models = [$model];
			$this->findWith($this->with, $models);
			$model = $models[0];
		}
		return $model;
139
	}
140

141
	/**
Qiang Xue committed
142
	 * @inheritdoc
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	 */
	public function search($db = null, $options = [])
	{
		$result = $this->createCommand($db)->search($options);
		if (!empty($result['hits']['hits'])) {
			$models = $this->createModels($result['hits']['hits']);
			if ($this->asArray) {
				foreach($models as $key => $model) {
					$model['_source'][ActiveRecord::PRIMARY_KEY_NAME] = $model['_id'];
					$models[$key] = $model['_source'];
				}
			}
			if (!empty($this->with)) {
				$this->findWith($this->with, $models);
			}
			$result['hits']['hits'] = $models;
		}
		return $result;
	}

163
	/**
Qiang Xue committed
164
	 * @inheritdoc
165 166 167 168 169
	 */
	public function scalar($field, $db = null)
	{
		$record = parent::one($db);
		if ($record !== false) {
170
			if ($field == ActiveRecord::PRIMARY_KEY_NAME) {
171 172 173 174 175 176 177 178 179
				return $record['_id'];
			} elseif (isset($record['_source'][$field])) {
				return $record['_source'][$field];
			}
		}
		return null;
	}

	/**
Qiang Xue committed
180
	 * @inheritdoc
181 182 183
	 */
	public function column($field, $db = null)
	{
184
		if ($field == ActiveRecord::PRIMARY_KEY_NAME) {
185 186
			$command = $this->createCommand($db);
			$command->queryParts['fields'] = [];
187 188 189 190 191 192 193
			$result = $command->search();
			if (empty($result['hits']['hits'])) {
				return [];
			}
			$column = [];
			foreach ($result['hits']['hits'] as $row) {
				$column[] = $row['_id'];
194
			}
195
			return $column;
196 197 198
		}
		return parent::column($field, $db);
	}
199
}