Skip to content

Instantly share code, notes, and snippets.

@infario
Last active September 2, 2017 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save infario/d7e9817174126bb601eb7673f948a04e to your computer and use it in GitHub Desktop.
Save infario/d7e9817174126bb601eb7673f948a04e to your computer and use it in GitHub Desktop.
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