/** * Converts the raw query results into the format as specified by this query. * This method is internally used to convert the data fetched from database * into the format as required by this query. * @param array $rows the raw query result from database * @return array the converted query result */ public function populate(array $rows) { $result = []; foreach ($rows as $file) { $row = $file->file; $row['file'] = $file; $result[] = $row; } return parent::populate($result); }
/** * @inheritdoc */ public function getMulti(array $keys) { $keys = $this->prepareKeys($keys); $query = new Query(); $rows = $query->select(['id', 'value'])->from($this->cacheCollection)->where(['id' => ['$in' => $keys], '$or' => [['expire' => null], ['expire' => ['$gt' => new \MongoDate()]]]])->indexBy('id')->all($this->storage); if (empty($rows)) { return []; } $result = []; foreach ($rows as $key => $value) { $result[$key] = $value['value'] === '' ? null : $value['value']; } return $result; }
/** * Performs 'findAndModify' query and returns a single row of result. * Warning: in case 'new' option is set to 'false' (which is by default) usage of this method may lead * to unexpected behavior at some Active Record features, because object will be populated by outdated data. * * @param array $update update criteria * @param array $options list of options in format: optionName => optionValue. * @param ConnectionInterface $connection the Mongo connection used to execute the query. * @return ActiveRecord|array|null the original document, or the modified document when $options['new'] is set. * Depending on the setting of {@see \rock\db\ActiveQueryTrait::$asArray}, the query result may be either an array or an ActiveRecord object. * Null will be returned if the query results in nothing. */ public function modify(array $update, array $options = [], ConnectionInterface $connection = null) { $row = parent::modify($update, $options, $connection); if ($row !== null) { $models = $this->populate([$row]); return reset($models) ?: null; } else { return null; } }