Class yii\behaviors\BlameableBehavior

Inheritanceyii\behaviors\BlameableBehavior » yii\behaviors\AttributeBehavior » yii\base\Behavior » yii\base\Object
Implementsyii\base\Configurable
Subclasseslispa\amos\core\behaviors\BlameableBehavior
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/behaviors/BlameableBehavior.php

BlameableBehavior automatically fills the specified attributes with the current user ID.

To use BlameableBehavior, insert the following code to your ActiveRecord class:

use yii\behaviors\BlameableBehavior;

public function behaviors()
{
    return [
        BlameableBehavior::className(),
    ];
}

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.

Because attribute values will be set automatically by this behavior, they are usually not user input and should therefore not be validated, i.e. created_by and updated_by should not appear in the rules() method of the model.

If your attribute names are different, you may configure the $createdByAttribute and $updatedByAttribute properties like the following:

public function behaviors()
{
    return [
        [
            'class' => BlameableBehavior::className(),
            'createdByAttribute' => 'author_id',
            'updatedByAttribute' => 'updater_id',
        ],
    ];
}

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$attributes array List of attributes that are to be automatically filled with the value specified via $value. yii\behaviors\AttributeBehavior
$createdByAttribute 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. yii\behaviors\BlameableBehavior
$owner yii\base\Component|null The owner of this behavior yii\base\Behavior
$skipUpdateOnClean boolean Whether to skip this behavior when the $owner has not been modified yii\behaviors\AttributeBehavior
$updatedByAttribute 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. yii\behaviors\BlameableBehavior
$value mixed The value that will be assigned to the current attributes. yii\behaviors\BlameableBehavior

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Object
__construct() Constructor. yii\base\Object
__get() Returns the value of an object property. yii\base\Object
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Object
__set() Sets value of an object property. yii\base\Object
__unset() Sets an object property to null. yii\base\Object
attach() Attaches the behavior object to the component. yii\base\Behavior
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Object
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Object
className() Returns the fully qualified name of this class. yii\base\Object
detach() Detaches the behavior object from the component. yii\base\Behavior
evaluateAttributes() Evaluates the attribute value and assigns it to the current attributes. yii\behaviors\AttributeBehavior
events() Declares event handlers for the $owner's events. yii\behaviors\AttributeBehavior
hasMethod() Returns a value indicating whether a method is defined. yii\base\Object
hasProperty() Returns a value indicating whether a property is defined. yii\base\Object
init() Initializes the object. yii\behaviors\BlameableBehavior

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
getValue() Returns the value for the current attributes. yii\behaviors\BlameableBehavior

Property Details

$createdByAttribute public property

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 string $createdByAttribute 'created_by'
$updatedByAttribute public property

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 string $updatedByAttribute 'updated_by'
$value public property

The value that will be assigned to the current attributes. This can be an anonymous function, callable in array format (e.g. [$this, 'methodName']), an Expression object representing a DB expression (e.g. new Expression('NOW()')), scalar, string or an arbitrary value. If the former, the return value of the function will be assigned to the attributes. The signature of the function should be as follows,

function ($event)
{
    // return value will be assigned to the attribute
}

In case, when the property is null, the value of Yii::$app->user->id will be used as the value.

public mixed $value null

Method Details

getValue() protected method

Returns the value for the current attributes.

This method is called by evaluateAttributes(). Its return value will be assigned to the attributes corresponding to the triggering event.

In case, when the $value property is null, the value of Yii::$app->user->id will be used as the value.

protected mixed getValue ( $event )
$event yii\base\Event

The event that triggers the current attribute updating.

return mixed

The attribute value

init() public method

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )