Esempio n. 1
0
 public function isAttributeChanged($name)
 {
     if ($name == 'picUrl') {
         return $this->_old_picUrl != $this->picUrl;
     }
     return parent::isAttributeChanged($name);
 }
Esempio n. 2
0
 /**
  * @param string $name
  * @param bool $identical
  *
  * @return bool
  */
 public function isAttributeChanged($name, $identical = true)
 {
     if (in_array($name, $this->getMultiLangFields())) {
         $language = \Yii::$app->language;
         $name = $this->buildMultilangFieldName($name, $language);
     }
     return parent::isAttributeChanged($name, $identical);
 }
Esempio n. 3
0
 /**
  * @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;
 }
 /**
  * Метод срабатывает в момент обновления существующей записи моедли.
  */
 public function beforeUpdate()
 {
     if (in_array($this->owner->scenario, $this->scenarios)) {
         foreach ($this->attributes as $attribute) {
             if ($this->owner->isAttributeChanged($attribute)) {
                 $fileTempPath = $this->getTempPath($attribute);
                 if (is_file($fileTempPath)) {
                     rename($fileTempPath, $this->getPath($attribute));
                     if ($this->deleteOnSave === true and $this->owner->getOldAttribute($attribute)) {
                         $this->delete($attribute, true);
                     }
                     $this->triggerEventAfterUpload();
                 } else {
                     $this->owner->setAttribute($attribute, $this->owner->getOldAttribute($attribute));
                 }
             }
         }
     }
     // Удаляем указаные атрибуты и их файлы если это нужно
     if (!empty($this->deleteScenarios) and in_array($this->owner->scenario, $this->deleteScenarios)) {
         foreach ($this->deleteScenarios as $attribute => $scenario) {
             if ($this->owner->scenario === $scenario) {
                 $file = $this->getPath($attribute);
                 if (is_file($file) and unlink($file)) {
                     $this->owner->{$attribute} = null;
                 }
             }
         }
     }
 }