Yii2 creator behavior
Creator behavior for Yii2, (*1)
Installation
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
php composer.phar require --prefer-dist tugmaks/yii2-creator-behavior "*"
or add, (*4)
"tugmaks/yii2-creator-behavior": "*"
to the require section of your composer.json
file., (*5)
Usage
CreatorBehavior automatically fills the specified attributes with the current user id., (*6)
To use CreatorBehavior, insert the following code to your ActiveRecord class:, (*7)
use tugmaks\behaviors\CreatorBehavior;
public function behaviors()
{
return [
CreatorBehavior::className(),
];
}
By default, CreatorBehavior will fill the created_by
and updated_by
attributes with the current user id
when the associated AR object is being inserted; it will fill the updated_by
attribute
with the current user id when the AR object is being updated. The user id value is obtained by Yii::$app->user->id
., (*8)
For the above implementation to work with MySQL database, please declare the columns(created_by
, updated_by
) as int(11)., (*9)
If your attribute names are different or you want to use a different way of retrieving user id,
you may configure the [[createdByAttribute]], [[updatedByAttribute]] and [[value]] properties like the following:, (*10)
public function behaviors()
{
return [
[
'class' => CreatorBehavior::className(),
'createdByAttribute' => 'creator_id',
'updatedByAttribute' => 'updater_id',
'value'=>function($event){
return \Yii::$app->user->identity->getCustomId();
}
],
];
}