mongosoft\file\UploadBehavior::beforeSave PHP Метод

beforeSave() публичный Метод

This method is called at the beginning of inserting or updating a record.
public beforeSave ( )
    public function beforeSave()
    {
        /** @var BaseActiveRecord $model */
        $model = $this->owner;
        if (in_array($model->scenario, $this->scenarios)) {
            if ($this->_file instanceof UploadedFile) {
                if (!$model->getIsNewRecord() && $model->isAttributeChanged($this->attribute)) {
                    if ($this->unlinkOnSave === true) {
                        $this->delete($this->attribute, true);
                    }
                }
                $model->setAttribute($this->attribute, $this->_file->name);
            } else {
                // Protect attribute
                unset($model->{$this->attribute});
            }
        } else {
            if (!$model->getIsNewRecord() && $model->isAttributeChanged($this->attribute)) {
                if ($this->unlinkOnSave === true) {
                    $this->delete($this->attribute, true);
                }
            }
        }
    }

Usage Example

 /**
  * @inheritdoc
  */
 public function beforeSave()
 {
     parent::beforeSave();
     if ($this->crop_changed && !empty($this->cropped_field)) {
         $this->delete($this->cropped_field, true);
         /** @var BaseActiveRecord $model */
         $model = $this->owner;
         $name = $model->getAttribute($this->attribute);
         if (empty($name)) {
             $model->setAttribute($this->attribute, $model->getOldAttribute($this->attribute));
         }
         $model->setAttribute($this->cropped_field, $this->getCropFileName($model->getAttribute($this->attribute)));
     }
 }
All Usage Examples Of mongosoft\file\UploadBehavior::beforeSave