ActiveQuery.php 4.27 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;
9
use Guzzle\Http\Client;
10 11
use yii\db\ActiveQueryInterface;
use yii\db\ActiveQueryTrait;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
use yii\helpers\Json;

/**
 * ActiveQuery represents a query associated with an Active Record class.
 *
 * ActiveQuery instances are usually created by [[ActiveRecord::find()]]
 * 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.
 * - [[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.
 * - [[exists()]]: returns a value indicating whether the query result has data or not.
 *
 * You can use query methods, such as [[where()]], [[limit()]] and [[orderBy()]] to customize the query options.
 *
 * 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
 */
49
class ActiveQuery extends Query implements ActiveQueryInterface
50
{
51
	use ActiveQueryTrait;
52 53

	/**
54 55 56 57
	 * 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.
58
	 */
59
	public function createCommand($db = null)
60
	{
61
		/** @var ActiveRecord $modelClass */
62 63 64
		$modelClass = $this->modelClass;
		if ($db === null) {
			$db = $modelClass::getDb();
65 66
		}

67 68 69 70 71 72
		if ($this->type === null) {
			$this->type = $modelClass::type();
		}
		if ($this->index === null) {
			$this->index = $modelClass::index();
			$this->type = $modelClass::type();
73
		}
74 75
		$query = $db->getQueryBuilder()->build($this);
		return $db->createCommand($query, $this->index, $this->type);
76 77 78
	}

	/**
79 80 81 82
	 * 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.
83
	 */
84
	public function all($db = null)
85
	{
86
		$command = $this->createCommand($db);
87
		$result = $command->queryAll();
Carsten Brandt committed
88
		if (empty($result['hits'])) {
89 90 91
			return [];
		}
		$models = $this->createModels($result['hits']);
92 93 94 95 96 97
		if ($this->asArray) {
			foreach($models as $key => $model) {
				$models[$key] = $model['_source'];
				$models[$key]['primaryKey'] = $model['_id'];
			}
		}
98 99
		if (!empty($this->with)) {
			$this->findWith($this->with, $models);
100
		}
101
		return $models;
102 103 104
	}

	/**
105 106 107 108 109 110
	 * 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.
111
	 */
112
	public function one($db = null)
113
	{
Carsten Brandt committed
114
		if (($result = parent::one($db)) === false) {
115 116
			return null;
		}
117
		if ($this->asArray) {
Carsten Brandt committed
118 119
			$model = $result['_source'];
			$model['primaryKey'] = $result['_id'];
120 121 122
		} else {
			/** @var ActiveRecord $class */
			$class = $this->modelClass;
Carsten Brandt committed
123
			$model = $class::create($result);
124 125 126 127 128 129 130
		}
		if (!empty($this->with)) {
			$models = [$model];
			$this->findWith($this->with, $models);
			$model = $models[0];
		}
		return $model;
131 132
	}
}