populateRecord() public static method

This is an internal method meant to be called to create active record objects after fetching data from the database. It is mainly used by ActiveQuery to populate the query results into active records. When calling this method manually you should call BaseActiveRecord::afterFind on the created record to trigger the [[EVENT_AFTER_FIND|afterFind Event]].
public static populateRecord ( BaseActiveRecord $record, array $row )
$record BaseActiveRecord the record to be populated. In most cases this will be an instance created by [[instantiate()]] beforehand.
$row array attribute values (name => value)
Example #1
0
 /**
  * @inheritdoc
  */
 public static function populateRecord($record, $row)
 {
     $columns = static::getTableSchema()->columns;
     foreach ($row as $name => $value) {
         if (isset($columns[$name])) {
             $row[$name] = $columns[$name]->phpTypecast($value);
         }
     }
     parent::populateRecord($record, $row);
 }
Example #2
0
 /**
  * Populates an active record object using a xunsearch result document
  *
  * @param ActiveRecord $record the record to be populated.
  * @param \XSDocument $doc
  */
 public static function populateRecord($record, $doc)
 {
     parent::populateRecord($record, $doc->getFields());
     $record->setInternalDoc($doc);
 }
Example #3
0
 /**
  * @inheritdoc
  */
 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 TODO use field metadata for this
         foreach ($row['fields'] as $key => $value) {
             if (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...
 }
Example #4
0
 /**
  * @inheritdoc
  */
 public static function populateRecord($record, $row)
 {
     $columns = static::getIndexSchema()->columns;
     foreach ($row as $name => $value) {
         if (isset($columns[$name]) && $columns[$name]->isMva) {
             $row[$name] = explode(',', $value);
         }
     }
     parent::populateRecord($record, $row);
 }
 /**
  * Populates an active record object using a row of data from the database/storage.
  *
  * This is an internal method meant to be called to create active record objects after
  * fetching data from the database. It is mainly used by [[ActiveQuery]] to populate
  * the query results into active records.
  *
  * When calling this method manually you should call [[afterFind()]] on the created
  * record to trigger the [[EVENT_AFTER_FIND|afterFind Event]].
  *
  * @param static $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).
  * @return void
  */
 public static function populateRecord($record, $row)
 {
     $responseData = ArrayHelper::getValue($row, Query::RESPONSE_KEY_PARAM);
     unset($row[Query::RESPONSE_KEY_PARAM]);
     parent::populateRecord($record, $row);
     if (!empty($responseData)) {
         $record->_responseData = $responseData;
     }
 }
Example #6
0
 /**
  * @inheritdoc
  */
 public static function populateRecord($record, $row)
 {
     $columns = static::getIndexSchema()->columns;
     foreach ($row as $name => $value) {
         if (isset($columns[$name])) {
             if ($columns[$name]->isMva) {
                 $mvaValue = explode(',', $value);
                 $row[$name] = array_map([$columns[$name], 'phpTypecast'], $mvaValue);
             } else {
                 $row[$name] = $columns[$name]->phpTypecast($value);
             }
         }
     }
     parent::populateRecord($record, $row);
 }
 /**
  * @inheritdoc
  * @throws \yii\base\UnknownPropertyException
  */
 public static function populateRecord($record, $row)
 {
     $attributes = array_flip($record->attributes());
     foreach ($attributes as $attributeName => $attributeValue) {
         if (!array_key_exists($attributeName, $row)) {
             throw new UnknownPropertyException("Attribute `{$attributeName}` not found in API response. Available fields: " . implode(', ', array_keys($row)) . '.');
         }
     }
     parent::populateRecord($record, $row);
 }
 /**
  * @param BaseActiveRecord $record
  * @param array $row
  */
 public static function populateRecord($record, $row)
 {
     // setup @class if not exists - required attribute
     if (is_array($row) && empty($row['@class'])) {
         Yii::info('Setup required empty @class(`' . $record::tableName() . '`) for record');
         $row['@class'] = $record::tableName();
     }
     BaseActiveRecord::populateRecord($record, $row);
 }
Example #9
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']);
     //        }
     $attributes = $row;
     // just a plain document returned as array from query
     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->_attachments = isset($row['_attachments']) ? $row['_attachments'] : [];
     $record->_rev = isset($row['_rev']) ? $row['_rev'] : null;
     // TODO version should always be available...
 }
 public static function populateRecord($record, $row)
 {
     $attributes = [];
     if (!empty($row)) {
         $arrayAttributes = $record->arrayAttributes();
         foreach ($row as $key => $value) {
             if (!isset($arrayAttributes[$key])) {
                 $row[$key] = $value;
             }
         }
         $attributes = array_merge($attributes, $row);
     }
     parent::populateRecord($record, $attributes);
     $pk = static::primaryKey()[0];
     if ($pk === 'objectId') {
         $record->_id = $row['objectId'];
     }
 }