Commit c303ffd6 by Carsten Brandt

added ActiveRecord::arrayAttributes to elasticsearch

fixes #3381
parent 1b4d894c
...@@ -64,11 +64,13 @@ use yii\db\ActiveRelationTrait; ...@@ -64,11 +64,13 @@ use yii\db\ActiveRelationTrait;
* This methods may only be called in a relational context. Same is true for [[inverseOf()]], which * This methods may only be called in a relational context. Same is true for [[inverseOf()]], which
* marks a relation as inverse of another relation. * marks a relation as inverse of another relation.
* *
* > NOTE: elasticsearch limits the number of records returned by any query to 10 records by default. * > Note: elasticsearch limits the number of records returned by any query to 10 records by default.
* > If you expect to get more records you should specify limit explicitly in relation definition. * > If you expect to get more records you should specify limit explicitly in relation definition.
* > This is also important for relations that use [[via()]] so that if via records are limited to 10 * > This is also important for relations that use [[via()]] so that if via records are limited to 10
* > the relations records can also not be more than 10. * > the relations records can also not be more than 10.
* *
* > Note: Currently [[with]] is not supported in combination with [[asArray]].
*
* @author Carsten Brandt <mail@cebe.cc> * @author Carsten Brandt <mail@cebe.cc>
* @since 2.0 * @since 2.0
*/ */
......
...@@ -270,6 +270,7 @@ class ActiveRecord extends BaseActiveRecord ...@@ -270,6 +270,7 @@ class ActiveRecord extends BaseActiveRecord
* for the `_id` field so that it is part of the `_source` fields and thus becomes part of the attributes. * for the `_id` field so that it is part of the `_source` fields and thus becomes part of the attributes.
* *
* @return string[] list of attribute names. * @return string[] list of attribute names.
* @throws \yii\base\InvalidConfigException if not overridden in a child class.
*/ */
public function attributes() public function attributes()
{ {
...@@ -277,6 +278,19 @@ class ActiveRecord extends BaseActiveRecord ...@@ -277,6 +278,19 @@ class ActiveRecord extends BaseActiveRecord
} }
/** /**
* A list of attributes that should be treated as array valued when retrieved through [[ActiveQuery::fields]].
*
* If not listed by this method, attributes retrieved through [[ActiveQuery::fields]] will converted to a scalar value
* when the result array contains only one value.
*
* @return string[] list of attribute names. Must be a subset of [[attributes()]].
*/
public function arrayAttributes()
{
return [];
}
/**
* @return string the name of the index this record is stored in. * @return string the name of the index this record is stored in.
*/ */
public static function index() public static function index()
...@@ -294,6 +308,10 @@ class ActiveRecord extends BaseActiveRecord ...@@ -294,6 +308,10 @@ class ActiveRecord extends BaseActiveRecord
/** /**
* @inheritdoc * @inheritdoc
*
* @param ActiveRecord $record the record to be populated. In most cases this will be an instance
* created by [[instantiate()]] beforehand.
* @param array $row attribute values (name => value)
*/ */
public static function populateRecord($record, $row) public static function populateRecord($record, $row)
{ {
...@@ -302,9 +320,10 @@ class ActiveRecord extends BaseActiveRecord ...@@ -302,9 +320,10 @@ class ActiveRecord extends BaseActiveRecord
$attributes = $row['_source']; $attributes = $row['_source'];
} }
if (isset($row['fields'])) { if (isset($row['fields'])) {
// reset fields in case it is scalar value TODO use field metadata for this // reset fields in case it is scalar value
$arrayAttributes = $record->arrayAttributes();
foreach($row['fields'] as $key => $value) { foreach($row['fields'] as $key => $value) {
if (count($value) == 1) { if (!isset($arrayAttributes[$key]) && count($value) == 1) {
$row['fields'][$key] = reset($value); $row['fields'][$key] = reset($value);
} }
} }
...@@ -439,6 +458,7 @@ class ActiveRecord extends BaseActiveRecord ...@@ -439,6 +458,7 @@ class ActiveRecord extends BaseActiveRecord
* @param array $condition the conditions that will be put in the WHERE part of the UPDATE SQL. * @param array $condition the conditions that will be put in the WHERE part of the UPDATE SQL.
* Please refer to [[ActiveQuery::where()]] on how to specify this parameter. * Please refer to [[ActiveQuery::where()]] on how to specify this parameter.
* @return integer the number of rows updated * @return integer the number of rows updated
* @throws Exception on error.
*/ */
public static function updateAll($attributes, $condition = []) public static function updateAll($attributes, $condition = [])
{ {
...@@ -498,6 +518,7 @@ class ActiveRecord extends BaseActiveRecord ...@@ -498,6 +518,7 @@ class ActiveRecord extends BaseActiveRecord
* @param string|array $condition the conditions that will be put in the WHERE part of the UPDATE SQL. * @param string|array $condition the conditions that will be put in the WHERE part of the UPDATE SQL.
* Please refer to [[Query::where()]] on how to specify this parameter. * Please refer to [[Query::where()]] on how to specify this parameter.
* @return integer the number of rows updated * @return integer the number of rows updated
* @throws Exception on error.
*/ */
public static function updateAllCounters($counters, $condition = []) public static function updateAllCounters($counters, $condition = [])
{ {
...@@ -562,6 +583,7 @@ class ActiveRecord extends BaseActiveRecord ...@@ -562,6 +583,7 @@ class ActiveRecord extends BaseActiveRecord
* @param array $condition the conditions that will be put in the WHERE part of the DELETE SQL. * @param array $condition the conditions that will be put in the WHERE part of the DELETE SQL.
* Please refer to [[ActiveQuery::where()]] on how to specify this parameter. * Please refer to [[ActiveQuery::where()]] on how to specify this parameter.
* @return integer the number of rows deleted * @return integer the number of rows deleted
* @throws Exception on error.
*/ */
public static function deleteAll($condition = []) public static function deleteAll($condition = [])
{ {
......
...@@ -4,7 +4,7 @@ Yii Framework 2 elasticsearch extension Change Log ...@@ -4,7 +4,7 @@ Yii Framework 2 elasticsearch extension Change Log
2.0.0 under development 2.0.0 under development
----------------------- -----------------------
- no changes in this release. - Enh #3381: Added ActiveRecord::arrayAttributes() to define attributes that should be treated as array when retrieved via `fields` (cebe)
2.0.0-rc September 27, 2014 2.0.0-rc September 27, 2014
......
...@@ -320,7 +320,13 @@ class Query extends Component implements QueryInterface ...@@ -320,7 +320,13 @@ class Query extends Component implements QueryInterface
} }
$column = []; $column = [];
foreach ($result['hits']['hits'] as $row) { foreach ($result['hits']['hits'] as $row) {
$column[] = isset($row['_source'][$field]) ? $row['_source'][$field] : null; if (isset($row['fields'][$field])) {
$column[] = $row['fields'][$field];
} elseif (isset($row['_source'][$field])) {
$column[] = $row['_source'][$field];
} else {
$column[] = null;
}
} }
return $column; return $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