/** * @param ActiveRecord $model * @param array $opts * @return ActiveQuery | array */ static function searchQuery($model, $opts = []) { $opts = ArrayHelper::merge(['data' => null, 'query' => null, 'columns' => [], 'filters' => []], $opts); $columns = $opts['columns']; $filters = $opts['filters']; $data = $opts['data']; if (null === $data) { $data = \Yii::$app->request->get(); } $query = $opts['query']; if (is_string($query)) { $query = call_user_func([$model, $opts['query']]); } elseif (null === $query) { $query = $model->find(); foreach (array_filter($model->getAttributes()) as $prop => $val) { $query->andWhere([$prop => $val]); } } if ($model->load($data) && $model->validate()) { foreach ($model->getAttributes($model->safeAttributes()) as $name => $value) { if ($model->isAttributeChanged($name)) { $attributeTypes = []; if (method_exists($model, 'attributeTypes')) { $attributeTypes = $model->attributeTypes(); } $type = null; if (isset($attributeTypes[$name])) { $type = $attributeTypes[$name]; } // Default filter function $filterFunc = isset($filters[$name]) && is_callable($filters[$name]) ? $filters[$name] : function (ActiveQuery $query, $name, $value, $type) { /** * @var string $name * @var string|array $value * @var string $type */ $query->andFilterWhere(static::searchAttribute($name, $value, $type)); }; if (isset($columns[$name])) { $name = $columns[$name]; } call_user_func($filterFunc, $query, $name, $value, $type); } } } return $query; }
/** * Get an array representing the properties of a model. * * @param \yii\db\ActiveRecord $model * @return array */ public static function castModel(ActiveRecord $model) { $attributes = array_merge($model->getAttributes(), $model->getRelatedRecords()); $results = []; foreach ($attributes as $key => $value) { $results[Caster::PREFIX_VIRTUAL . $key] = $value; } return $results; }
/** * Store data record and track change statistics * * @param ActiveRecord $ActiveRecord * * @return bool false if not saved */ protected function storeDataRecord(ActiveRecord $ActiveRecord) { if ($ActiveRecord->getDirtyAttributes()) { $unsaved_record = clone $ActiveRecord; // Save record if (!$ActiveRecord->save()) { // Create error message $message = "Save error: " . json_encode($ActiveRecord->errors) . "\n"; $message .= "Record data: " . json_encode($ActiveRecord->getAttributes()) . "\n"; trigger_error($message, E_USER_WARNING); $this->incStat('error_' . $ActiveRecord->tableName()); return false; } // Store statistics if ($unsaved_record->isNewRecord) { $this->incStat('new_' . $ActiveRecord->tableName()); } else { $this->incStat('update_' . $ActiveRecord->tableName()); } } return true; }
/** * Format same as kartik\builder\Form::$attributes * @param ActiveRecord $model * @return array * * @see kartik\builder\Form::$attributes */ public function getFormElements($model) { $res = []; $attributes = $model->getAttributes(); foreach ($model->getTableSchema()->primaryKey as $pk) { unset($attributes[$pk]); } $attributes = array_keys($attributes); foreach ($attributes as $attribute) { $res[$attribute]['type'] = Form::INPUT_TEXT; } $res[] = $this->getActionRow($model); return $res; }
/** * @param ActiveRecord $model * @param array $changedAttributes */ public static function dropCaches($model, array $changedAttributes = []) { self::initialize(); $attrs = $model->getAttributes(); $changed = []; if ($changedAttributes) { $attrNames = array_keys($changedAttributes); foreach ($attrNames as $attrName) { if (array_key_exists($attrName, $attrs)) { $changed[$attrName] = $attrs[$attrName]; } } } $args = [$model->tableName(), json_encode($attrs, self::$jsonOptions), json_encode($changed, self::$jsonOptions)]; CacheHelper::evalSHA(self::$shaInvalidate, $args, 0); }
public function getAttributes() { $attr = parent::getAttributes(); $attr['pingicon'] = $this->pingicon; return $attr; }
public function afterUpdate() { TimelineEvent::log($this->owner->className(), 'afterUpdate', ['attributes' => $this->owner->getAttributes(), 'uid' => Yii::$app->user->identity->id]); return true; }
/** * Populate attributes value */ public function populateAttributes() { $attributes = array_intersect_key($this->_source->getAttributes(), $this->getAttributes()); $this->setAttributes($attributes, false); }
public function init() { parent::init(); self::getConfig(); self::$_modelFields = array_keys(parent::getAttributes()); XiiVersion::run(self::XII_VERSION); }