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.
200 lines
5.8 KiB
200 lines
5.8 KiB
11 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\elasticsearch;
|
||
11 years ago
|
|
||
11 years ago
|
use yii\db\ActiveQueryInterface;
|
||
|
use yii\db\ActiveQueryTrait;
|
||
11 years ago
|
|
||
|
/**
|
||
11 years ago
|
* ActiveQuery represents a [[Query]] associated with an [[ActiveRecord]] class.
|
||
11 years ago
|
*
|
||
11 years ago
|
* ActiveQuery instances are usually created by [[ActiveRecord::find()]].
|
||
11 years ago
|
*
|
||
|
* 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.
|
||
11 years ago
|
* - [[column()]]: returns the value of the first column in the query result.
|
||
11 years ago
|
* - [[exists()]]: returns a value indicating whether the query result has data or not.
|
||
|
*
|
||
11 years ago
|
* Because ActiveQuery extends from [[Query]], one can use query methods, such as [[where()]],
|
||
|
* [[orderBy()]] to customize the query options.
|
||
11 years ago
|
*
|
||
|
* 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
|
||
|
*/
|
||
11 years ago
|
class ActiveQuery extends Query implements ActiveQueryInterface
|
||
11 years ago
|
{
|
||
11 years ago
|
use ActiveQueryTrait;
|
||
11 years ago
|
|
||
|
/**
|
||
11 years ago
|
* 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.
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function createCommand($db = null)
|
||
11 years ago
|
{
|
||
11 years ago
|
/** @var ActiveRecord $modelClass */
|
||
11 years ago
|
$modelClass = $this->modelClass;
|
||
|
if ($db === null) {
|
||
|
$db = $modelClass::getDb();
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
if ($this->type === null) {
|
||
|
$this->type = $modelClass::type();
|
||
|
}
|
||
|
if ($this->index === null) {
|
||
|
$this->index = $modelClass::index();
|
||
|
$this->type = $modelClass::type();
|
||
11 years ago
|
}
|
||
11 years ago
|
$commandConfig = $db->getQueryBuilder()->build($this);
|
||
|
return $db->createCommand($commandConfig);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* 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.
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function all($db = null)
|
||
11 years ago
|
{
|
||
11 years ago
|
$result = $this->createCommand($db)->search();
|
||
|
if (empty($result['hits']['hits'])) {
|
||
11 years ago
|
return [];
|
||
|
}
|
||
11 years ago
|
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) {
|
||
11 years ago
|
foreach($models as $key => $model) {
|
||
11 years ago
|
$model['_source'][ActiveRecord::PRIMARY_KEY_NAME] = $model['_id'];
|
||
11 years ago
|
$models[$key] = $model['_source'];
|
||
|
}
|
||
|
}
|
||
11 years ago
|
if (!empty($this->with)) {
|
||
|
$this->findWith($this->with, $models);
|
||
11 years ago
|
}
|
||
11 years ago
|
return $models;
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* 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.
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function one($db = null)
|
||
11 years ago
|
{
|
||
11 years ago
|
if (($result = parent::one($db)) === false) {
|
||
11 years ago
|
return null;
|
||
|
}
|
||
11 years ago
|
if ($this->asArray) {
|
||
11 years ago
|
$model = $result['_source'];
|
||
11 years ago
|
$model[ActiveRecord::PRIMARY_KEY_NAME] = $result['_id'];
|
||
11 years ago
|
} else {
|
||
|
/** @var ActiveRecord $class */
|
||
|
$class = $this->modelClass;
|
||
11 years ago
|
$model = $class::create($result);
|
||
11 years ago
|
}
|
||
|
if (!empty($this->with)) {
|
||
|
$models = [$model];
|
||
|
$this->findWith($this->with, $models);
|
||
|
$model = $models[0];
|
||
|
}
|
||
|
return $model;
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
11 years ago
|
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;
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function scalar($field, $db = null)
|
||
|
{
|
||
|
$record = parent::one($db);
|
||
|
if ($record !== false) {
|
||
11 years ago
|
if ($field == ActiveRecord::PRIMARY_KEY_NAME) {
|
||
11 years ago
|
return $record['_id'];
|
||
|
} elseif (isset($record['_source'][$field])) {
|
||
|
return $record['_source'][$field];
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
|
public function column($field, $db = null)
|
||
|
{
|
||
11 years ago
|
if ($field == ActiveRecord::PRIMARY_KEY_NAME) {
|
||
11 years ago
|
$command = $this->createCommand($db);
|
||
|
$command->queryParts['fields'] = [];
|
||
11 years ago
|
$result = $command->search();
|
||
|
if (empty($result['hits']['hits'])) {
|
||
|
return [];
|
||
|
}
|
||
|
$column = [];
|
||
|
foreach ($result['hits']['hits'] as $row) {
|
||
|
$column[] = $row['_id'];
|
||
11 years ago
|
}
|
||
11 years ago
|
return $column;
|
||
11 years ago
|
}
|
||
|
return parent::column($field, $db);
|
||
|
}
|
||
11 years ago
|
}
|