To use UploadBehavior, insert the following code to your ActiveRecord class:
php
use mongosoft\file\UploadBehavior;
function behaviors()
{
return [
[
'class' => UploadBehavior::className(),
'attribute' => 'file',
'scenarios' => ['insert', 'update'],
'path' => '@webroot/upload/{id}',
'url' => '@web/upload/{id}',
],
];
}
/** * @inheritdoc */ public function afterSave() { parent::afterSave(); if ($this->crop_changed) { $this->createCrop(); } }
/** * @inheritdoc */ protected function delete($attribute, $old = false) { parent::delete($attribute, $old); $profiles = array_keys($this->thumbs); foreach ($profiles as $profile) { $path = $this->getThumbUploadPath($attribute, $profile, $old); if (is_file($path)) { unlink($path); } } }
/** * @inheritdoc */ function behaviors() { return [['class' => UploadBehavior::className(), 'attribute' => 'file', 'scenarios' => ['insert', 'update'], 'path' => '@webroot/upload/docs/{id}', 'url' => '@web/upload/docs/{id}']]; }
/** * @inheritdoc */ public function afterSave() { parent::afterSave(); if (in_array($this->owner->scenario, $this->scenarios)) { $image = null; foreach ($this->getConfigurations() as $crop) { if (isset($crop['_changed']) && $crop['_changed']) { if (!$image) { $path = $this->getUploadPath($this->attribute); if (!$path) { $path = $this->getUploadPath($this->attribute, true); } $image = Image::getImagine()->open($path); } $this->createCrop($crop, $image->copy()); } } } }