Last active
September 2, 2017 09:24
-
-
Save infario/d7e9817174126bb601eb7673f948a04e to your computer and use it in GitHub Desktop.
https://github.com/mohorev/yii2-upload-behavior generateNewName
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Document extends ActiveRecord | |
{ | |
/** | |
* @inheritdoc | |
*/ | |
public function rules() | |
{ | |
return [ | |
['file', 'file', 'extensions' => 'doc, docx, pdf', 'on' => ['insert', 'update']], | |
]; | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getCategory() | |
{ | |
return $this->hasOne(Category::className(), [ 'id' => 'id_category' ]); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
function behaviors() | |
{ | |
return [ | |
[ | |
'class' => UploadBehavior::className(), | |
'attribute' => 'file', | |
'scenarios' => ['insert', 'update'], | |
'path' => '@webroot/upload/docs/{category.id}', | |
'url' => '@web/upload/docs/{category.id}', | |
'generateNewName' => function($file){ | |
$model = $this->owner; | |
return $model->id.'-'.$model->anyattribute.'.'.$file->extenstion; | |
} | |
], | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment