Commit 8d9f5b48 by Alexander Kochetov Committed by Qiang Xue

\yii\behaviors\BlameableBehavior updated

parent 471d0536
......@@ -14,7 +14,7 @@ use yii\db\BaseActiveRecord;
/**
* BlameableBehavior automatically fills the specified attributes with the current user ID.
*
* To use BlameableBehavior, simply insert the following code to your ActiveRecord class:
* To use BlameableBehavior, insert the following code to your ActiveRecord class:
*
* ```php
* use yii\behaviors\BlameableBehavior;
......@@ -30,7 +30,7 @@ use yii\db\BaseActiveRecord;
* By default, BlameableBehavior 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. If your attribute names are different, you may configure
* the [[attributes]] property like the following:
* the [[createdByAttribute]] and [[updatedByAttribute]] properties like the following:
*
* ```php
* public function behaviors()
......@@ -38,10 +38,8 @@ use yii\db\BaseActiveRecord;
* return [
* [
* 'class' => BlameableBehavior::className(),
* 'attributes' => [
* ActiveRecord::EVENT_BEFORE_INSERT => 'author_id',
* ActiveRecord::EVENT_BEFORE_UPDATE => 'updater_id',
* ],
* 'createdByAttribute' => 'author_id',
* 'updatedByAttribute' => 'updater_id',
* ],
* ];
* }
......@@ -49,22 +47,19 @@ use yii\db\BaseActiveRecord;
*
* @author Luciano Baraglia <luciano.baraglia@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alexander Kochetov <creocoder@gmail.com>
* @since 2.0
*/
class BlameableBehavior extends AttributeBehavior
{
/**
* @var array list of attributes that are to be automatically filled with the current user ID.
* The array keys are the ActiveRecord events upon which the attributes are to be filled with the user ID,
* and the array values are the corresponding attribute(s) to be updated. You can use a string to represent
* a single attribute, or an array to represent a list of attributes.
* The default setting is to update both of the `created_by` and `updated_by` attributes upon AR insertion,
* and update the `updated_by` attribute upon AR updating.
* @var string the attribute that will receive current user ID value
*/
public $attributes = [
BaseActiveRecord::EVENT_BEFORE_INSERT => ['created_by', 'updated_by'],
BaseActiveRecord::EVENT_BEFORE_UPDATE => 'updated_by',
];
public $createdByAttribute = 'created_by';
/**
* @var string the attribute that will receive current user ID value
*/
public $updatedByAttribute = 'updated_by';
/**
* @var callable the value that will be assigned to the attributes. This should be a valid
* PHP callable whose return value will be assigned to the current attribute(s).
......@@ -81,6 +76,21 @@ class BlameableBehavior extends AttributeBehavior
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,
];
}
}
/**
* Evaluates the value of the user.
* The return result of this method will be assigned to the current attribute(s).
* @param Event $event
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment