BlameableBehavior::className(), * 'createdByAttribute' => 'author_id', * 'updatedByAttribute' => 'updater_id', * ], * ]; * } * ``` * * @author Luciano Baraglia * @author Qiang Xue * @author Alexander Kochetov * @since 2.0 */ class BlameableBehavior extends AttributeBehavior { /** * @var string the attribute that will receive current user ID value * Set this property to false if you do not want to record the creator ID. */ public $createdByAttribute = 'created_by'; /** * @var string the attribute that will receive current user ID value * Set this property to false if you do not want to record the updater ID. */ public $updatedByAttribute = 'updated_by'; /** * @inheritdoc * * In case, when the property is `null`, the value of `Yii::$app->user->id` will be used as the value. */ public $value; /** * @inheritdoc */ public function init() { parent::init(); if (empty($this->attributes)) { $this->attributes = [ BaseActiveRecord::EVENT_BEFORE_INSERT => [$this->createdByAttribute, $this->updatedByAttribute], BaseActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedByAttribute, ]; } } /** * @inheritdoc * * In case, when the [[value]] property is `null`, the value of `Yii::$app->user->id` will be used as the value. */ protected function getValue($event) { if ($this->value === null) { $user = Yii::$app->get('user', false); return $user && !$user->isGuest ? $user->id : null; } return parent::getValue($event); } }