Class yii\behaviors\BlameableBehavior

Inheritanceyii\behaviors\BlameableBehavior » yii\behaviors\AttributeBehavior » yii\base\Behavior » yii\base\Object
Implementsyii\base\Configurable
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. 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 The owner of this behavior yii\base\Behavior
$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 callable The value that will be assigned to the 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() Evaluates the value of the user. yii\behaviors\BlameableBehavior

Property Details

$createdByAttribute public property
string $createdByAttribute 'created_by'

The attribute that will receive current user ID value Set this property to false if you do not want to record the creator ID.

$updatedByAttribute public property
string $updatedByAttribute 'updated_by'

The attribute that will receive current user ID value Set this property to false if you do not want to record the updater ID.

$value public property

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). The signature of the callable should be:

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

If this property is not set, the value of Yii::$app->user->id will be assigned to the attribute(s).

Method Details

getValue() protected method

Evaluates the value of the user.

The return result of this method will be assigned to the current attribute(s).

mixed getValue$event )
$event yii\base\Event
return mixed

The value of the user.

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.

void init( )