oleg-chulakov-studio/yii2-filesaver

Yii2 behavior for saving file in model

1.0.0 2017-07-12 13:44 UTC

This package is auto-updated.

Last update: 2024-10-20 01:26:44 UTC


README

Russian

Install by composer

composer require oleg-chulakov-studio/yii2-filesaver

Or add this code into require section of your composer.json and then call composer update in console

"oleg-chulakov-studio/yii2-filesaver": "*"

Usage

  • In model add behavior
    public function behaviors()
    {
        return [
            ....
            [
                'class' => FileSaverBehavior::className(),
                'group_type' => 'photo',
                'in_attribute' => 'photoFile',
                'out_attribute' => 'photo_id',
                'del_attribute' => 'photoDel',
                'allowedExtensions' => [
                    'png',
                    'jpeg',
                    'jpg'
                ]
            ],
            ...
        ];
    }
  • In model add fields
/**
 * @var UploadedFile
/**
$photoFile;

 /**
  * @var boolean
 /**
$photoDel;

Example usage

  • saving by url
$model = new TestModel();
$model->photoFile = new \sem\filestorage\adapters\RemoteFile($url);
$model->save();
  • saving by upload in form
$model = new TestModel();
$model->photoFile = UploadedFile::getInstance($model, 'photoFile');
$model->save();