Commit 679da533 by Carsten Brandt

polished Query API

parent c6347d6d
......@@ -42,7 +42,7 @@ class Query extends Component implements QueryInterface
/**
* @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()
*/
public $select;
......
......@@ -85,7 +85,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
{
$command = $this->createCommand($db);
$result = $command->queryAll();
if ($result['total'] == 0) {
if (empty($result['hits'])) {
return [];
}
$models = $this->createModels($result['hits']);
......@@ -111,19 +111,16 @@ class ActiveQuery extends Query implements ActiveQueryInterface
*/
public function one($db = null)
{
$command = $this->createCommand($db);
$result = $command->queryOne();
if ($result['total'] == 0 || empty($result['hits'])) {
if (($result = parent::one($db)) === false) {
return null;
}
if ($this->asArray) {
$first = reset($result['hits']);
$model = $first['_source'];
$model['primaryKey'] = $first['_id'];
$model = $result['_source'];
$model['primaryKey'] = $result['_id'];
} else {
/** @var ActiveRecord $class */
$class = $this->modelClass;
$model = $class::create(reset($result['hits']));
$model = $class::create($result);
}
if (!empty($this->with)) {
$models = [$model];
......@@ -132,24 +129,4 @@ class ActiveQuery extends Query implements ActiveQueryInterface
}
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
* @param mixed $primaryKey the primaryKey value
* @param array $options options given in this parameter are passed to elasticsearch
* as request URI parameters.
*
* Please refer to the [elasticsearch documentation](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-get.html)
* for more details on these options.
* @return static|null The record instance or null if it was not found.
......
......@@ -59,12 +59,6 @@ class Command extends Component
return Json::decode($response->getBody(true))['hits'];
}
public function queryOne($options = [])
{
$options['size'] = 1;
return $this->queryAll($options);
}
public function queryCount($options = [])
{
$options['search_type'] = 'count';
......
......@@ -45,7 +45,7 @@ class QueryBuilder extends \yii\base\Object
public function build($query)
{
$searchQuery = array();
$this->buildSelect($searchQuery, $query->select);
$this->buildFields($searchQuery, $query->fields);
// $this->buildFrom($searchQuery, $query->from);
$this->buildCondition($searchQuery, $query->where);
$this->buildOrderBy($searchQuery, $query->orderBy);
......@@ -113,9 +113,9 @@ class QueryBuilder extends \yii\base\Object
* @param string $selectOption
* @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;
}
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