tugmaks / yii2-creator-behavior
Creator behavior for Yii2
Installs: 268
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2025-04-24 21:52:56 UTC
README
Creator behavior for Yii2
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist tugmaks/yii2-creator-behavior "*"
or add
"tugmaks/yii2-creator-behavior": "*"
to the require section of your composer.json
file.
Usage
CreatorBehavior automatically fills the specified attributes with the current user id.
To use CreatorBehavior, insert the following code to your ActiveRecord class:
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
.
For the above implementation to work with MySQL database, please declare the columns(created_by
, updated_by
) as int(11).
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:
public function behaviors() { return [ [ 'class' => CreatorBehavior::className(), 'createdByAttribute' => 'creator_id', 'updatedByAttribute' => 'updater_id', 'value'=>function($event){ return \Yii::$app->user->identity->getCustomId(); } ], ]; }