Class yii\behaviors\BlameableBehavior

Inheritanceyii\behaviors\BlameableBehavior » yii\behaviors\AttributeBehavior » yii\base\Behavior » yii\base\BaseObject
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::class,
    ];
}

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::class,
            '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
$defaultValue mixed Default value for cases when the user is guest yii\behaviors\BlameableBehavior
$owner yii\base\Component|null The owner of this behavior yii\base\Behavior
$preserveNonEmptyValues boolean Whether to preserve non-empty attribute values. yii\behaviors\AttributeBehavior
$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\BaseObject
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
attach() Attaches the behavior object to the component. yii\base\Behavior
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
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\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\behaviors\BlameableBehavior

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
getDefaultValue() Get default value yii\behaviors\BlameableBehavior
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'
$defaultValue public property (available since version 2.0.14)

Default value for cases when the user is guest

public mixed $defaultValue null
$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

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

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
}
public mixed $value null

Method Details

getDefaultValue() protected method (available since version 2.0.14)

Get default value

protected array|mixed getDefaultValue ( $event )
$event yii\base\Event
getValue() protected method

Returns the value for the current attributes.

In case, when the $value property is null, the value of $defaultValue will be used as the value.

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

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 ( )