/** * @return bool * @throws \yii\base\Exception */ protected function saveFile() { if ($this->uploadFile && $this->uploadFile instanceof UploadedFile) { if (!file_exists($this->uploadDir)) { FileHelper::createDirectory($this->uploadDir, '0777'); } if ($this->uploadFile->saveAs($this->getFilePath($this->getFileName()))) { $this->owner->setAttribute($this->attribute, $this->getFileName()); return true; } } else { $this->owner->setAttribute($this->attribute, $this->owner->getOldAttribute($this->attribute)); } return false; }
/** * Handle 'afterFind' event of the owner. */ public function afterFind() { if ($this->owner->isRelationPopulated('translations') && ($related = $this->owner->getRelatedRecords()['translations'])) { $translations = $this->indexByLanguage($related); foreach ($this->availableLanguages as $lang) { foreach ($this->attributes as $attribute) { foreach ($translations as $translation) { if ($translation->{$this->languageField} == $lang) { $this->setTranslateAttribute($attribute . '_' . $lang, $translation->{$attribute}); if ($lang == Yii::$app->language) { $this->setTranslateAttribute($attribute, $translation->{$attribute}); } } } } } } else { if (!$this->owner->isRelationPopulated('translation')) { $this->owner->translation; } $translation = $this->owner->getRelatedRecords()['translation']; if ($translation) { foreach ($this->attributes as $attribute) { $this->owner->setTranslateAttribute($attribute, $translation->{$attribute}); } } } foreach ($this->attributes as $attribute) { if ($this->owner->hasAttribute($attribute) && $this->getTranslateAttribute($attribute)) { $this->owner->setAttribute($attribute, $this->getTranslateAttribute($attribute)); } } }
/** * @inheritdoc */ public function setAttribute($name, $value) { if ($this->tryToSetRelation($name, $value)) { return; } parent::setAttribute($name, $value); }
public function setAttribute($name, $value) { if (isset($this->{$name})) { $this->{$name} = $value; } else { parent::setAttribute($name, $value); } }
/** * Populate model data * @param ActiveRecord $model the model to populate * @param array $modelData * @return bool true if success */ public function loadModel($model, $modelData) { $model->trigger('beforeLoad'); if (empty($modelData)) { return false; } $attributes = $model->attributes(); $b = false; foreach ($attributes as $attrName) { if (array_key_exists($attrName, $modelData)) { $model->setAttribute($attrName, $modelData[$attrName]); $b = true; } } $model->trigger('afterLoad'); return $b; }
/** * Perform last checks and the actual state transition. * * @param \yii\db\ActiveRecord|IStateful $model * @param array $stateChange * @param mixed $sourceState * @param boolean $confirmed * * @return bool true if state transition has been performed */ public function performTransition($model, $stateChange, $sourceState, $confirmed = true) { // explicitly assign the new state value to avoid forcing the state attribute to be safe $model->setAttribute($model->getStateAttributeName(), $this->targetState); if (!$this->beforeTransition($model) || $model->performTransition() === false) { return false; } $this->afterTransition($model); return true; }
/** * Метод срабатывает в момент обновления существующей записи моедли. */ 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; } } } } }
public function setAttribute($name, $value) { if ($this->trySetupEmbeddedRelation($name, $value)) { // if success return; } parent::setAttribute($name, $value); }
public function setSort($position) { $this->owner->setAttribute($this->attributeName, $position); }
/** * Sets the named attribute value. * @param string $name * the attribute name * @param mixed $value * the attribute value. * @throws InvalidParamException if the named attribute does not exist. * @throws Exception if the current record is read only * @see hasAttribute() */ public function setAttribute($name, $value) { if ($this->getReadOnly()) { throw new Exception('Attempting to set attribute `' . $name . '` on a read only ' . Tools::getClassName($this) . ' model'); } parent::setAttribute($name, $value); }