yii\elasticsearch\ActiveRecord::arrayAttributes PHP Method

arrayAttributes() public method

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.
public arrayAttributes ( ) : string[]
return string[] list of attribute names. Must be a subset of [[attributes()]].
    public function arrayAttributes()
    {
        return [];
    }

Usage Example

Example #1
0
 /**
  * @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)
 {
     $attributes = [];
     if (isset($row['_source'])) {
         $attributes = $row['_source'];
     }
     if (isset($row['fields'])) {
         // reset fields in case it is scalar value
         $arrayAttributes = $record->arrayAttributes();
         foreach ($row['fields'] as $key => $value) {
             if (!isset($arrayAttributes[$key]) && count($value) == 1) {
                 $row['fields'][$key] = reset($value);
             }
         }
         $attributes = array_merge($attributes, $row['fields']);
     }
     parent::populateRecord($record, $attributes);
     $pk = static::primaryKey()[0];
     //TODO should always set ID in case of fields are not returned
     if ($pk === '_id') {
         $record->_id = $row['_id'];
     }
     $record->_highlight = isset($row['highlight']) ? $row['highlight'] : null;
     $record->_score = isset($row['_score']) ? $row['_score'] : null;
     $record->_version = isset($row['_version']) ? $row['_version'] : null;
     // TODO version should always be available...
 }