Commit 679da533 by Carsten Brandt

polished Query API

parent c6347d6d
...@@ -42,7 +42,7 @@ class Query extends Component implements QueryInterface ...@@ -42,7 +42,7 @@ class Query extends Component implements QueryInterface
/** /**
* @var array the columns being selected. For example, `['id', 'name']`. * @var array the columns being selected. For example, `['id', 'name']`.
* This is used to construct the SELECT clause in a SQL statement. If not set, if means selecting all columns. * This is used to construct the SELECT clause in a SQL statement. If not set, it means selecting all columns.
* @see select() * @see select()
*/ */
public $select; public $select;
......
...@@ -85,7 +85,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -85,7 +85,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
{ {
$command = $this->createCommand($db); $command = $this->createCommand($db);
$result = $command->queryAll(); $result = $command->queryAll();
if ($result['total'] == 0) { if (empty($result['hits'])) {
return []; return [];
} }
$models = $this->createModels($result['hits']); $models = $this->createModels($result['hits']);
...@@ -111,19 +111,16 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -111,19 +111,16 @@ class ActiveQuery extends Query implements ActiveQueryInterface
*/ */
public function one($db = null) public function one($db = null)
{ {
$command = $this->createCommand($db); if (($result = parent::one($db)) === false) {
$result = $command->queryOne();
if ($result['total'] == 0 || empty($result['hits'])) {
return null; return null;
} }
if ($this->asArray) { if ($this->asArray) {
$first = reset($result['hits']); $model = $result['_source'];
$model = $first['_source']; $model['primaryKey'] = $result['_id'];
$model['primaryKey'] = $first['_id'];
} else { } else {
/** @var ActiveRecord $class */ /** @var ActiveRecord $class */
$class = $this->modelClass; $class = $this->modelClass;
$model = $class::create(reset($result['hits'])); $model = $class::create($result);
} }
if (!empty($this->with)) { if (!empty($this->with)) {
$models = [$model]; $models = [$model];
...@@ -132,24 +129,4 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -132,24 +129,4 @@ class ActiveQuery extends Query implements ActiveQueryInterface
} }
return $model; return $model;
} }
/**
* Returns the query result as a scalar value.
* The value returned will be the specified attribute in the first record of the query results.
* @param string $attribute name of the attribute to select
* @param Connection $db the database connection used to execute the query.
* If this parameter is not given, the `db` application component will be used.
* @return string the value of the specified attribute in the first record of the query result.
* Null is returned if the query result is empty.
*/
public function scalar($attribute, $db = null)
{
$record = $this->one($db);
if ($record !== null) {
return $record->$attribute;
} else {
return null;
}
}
} }
...@@ -64,7 +64,6 @@ class ActiveRecord extends \yii\db\ActiveRecord ...@@ -64,7 +64,6 @@ class ActiveRecord extends \yii\db\ActiveRecord
* @param mixed $primaryKey the primaryKey value * @param mixed $primaryKey the primaryKey value
* @param array $options options given in this parameter are passed to elasticsearch * @param array $options options given in this parameter are passed to elasticsearch
* as request URI parameters. * as request URI parameters.
*
* Please refer to the [elasticsearch documentation](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-get.html) * Please refer to the [elasticsearch documentation](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-get.html)
* for more details on these options. * for more details on these options.
* @return static|null The record instance or null if it was not found. * @return static|null The record instance or null if it was not found.
......
...@@ -59,12 +59,6 @@ class Command extends Component ...@@ -59,12 +59,6 @@ class Command extends Component
return Json::decode($response->getBody(true))['hits']; return Json::decode($response->getBody(true))['hits'];
} }
public function queryOne($options = [])
{
$options['size'] = 1;
return $this->queryAll($options);
}
public function queryCount($options = []) public function queryCount($options = [])
{ {
$options['search_type'] = 'count'; $options['search_type'] = 'count';
......
...@@ -45,7 +45,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -45,7 +45,7 @@ class QueryBuilder extends \yii\base\Object
public function build($query) public function build($query)
{ {
$searchQuery = array(); $searchQuery = array();
$this->buildSelect($searchQuery, $query->select); $this->buildFields($searchQuery, $query->fields);
// $this->buildFrom($searchQuery, $query->from); // $this->buildFrom($searchQuery, $query->from);
$this->buildCondition($searchQuery, $query->where); $this->buildCondition($searchQuery, $query->where);
$this->buildOrderBy($searchQuery, $query->orderBy); $this->buildOrderBy($searchQuery, $query->orderBy);
...@@ -113,9 +113,9 @@ class QueryBuilder extends \yii\base\Object ...@@ -113,9 +113,9 @@ class QueryBuilder extends \yii\base\Object
* @param string $selectOption * @param string $selectOption
* @return string the SELECT clause built from [[query]]. * @return string the SELECT clause built from [[query]].
*/ */
public function buildSelect(&$query, $columns) public function buildFields(&$query, $columns)
{ {
if (empty($columns)) { if ($columns === null) {
return; return;
} }
foreach ($columns as $i => $column) { foreach ($columns as $i => $column) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment