PHP Class yii\behaviors\AttributeBehavior

To use AttributeBehavior, configure the [[attributes]] property which should specify the list of attributes that need to be updated and the corresponding events that should trigger the update. Then configure the [[value]] property with a PHP callable whose return value will be used to assign to the current attribute(s). For example, php use yii\behaviors\AttributeBehavior; public function behaviors() { return [ [ 'class' => AttributeBehavior::className(), 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => 'attribute1', ActiveRecord::EVENT_BEFORE_UPDATE => 'attribute2', ], 'value' => function ($event) { return 'some value'; }, ], ]; } Because attribute values will be set automatically by this behavior, they are usually not user input and should therefore not be validated, i.e. they should not appear in the [[\yii\base\Model::rules()|rules()]] method of the model.
Since: 2.0
Author: Luciano Baraglia ([email protected])
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\Behavior
Mostra file Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$attributes list of attributes that are to be automatically filled with the value specified via [[value]]. The array keys are the ActiveRecord events upon which the attributes are to be updated, 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. For example, php [ ActiveRecord::EVENT_BEFORE_INSERT => ['attribute1', 'attribute2'], ActiveRecord::EVENT_BEFORE_UPDATE => 'attribute2', ]
$skipUpdateOnClean whether to skip this behavior when the $owner has not been modified
$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 [[\yii\db\Expression|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, php function ($event) { return value will be assigned to the attribute }

Public Methods

Method Description
evaluateAttributes ( Event $event ) Evaluates the attribute value and assigns it to the current attributes.
events ( )

Protected Methods

Method Description
getValue ( Event $event ) : mixed Returns the value for the current attributes.

Method Details

evaluateAttributes() public method

Evaluates the attribute value and assigns it to the current attributes.
public evaluateAttributes ( Event $event )
$event yii\base\Event

events() public method

public events ( )

getValue() protected method

This method is called by AttributeBehavior::evaluateAttributes. Its return value will be assigned to the attributes corresponding to the triggering event.
protected getValue ( Event $event ) : mixed
$event yii\base\Event the event that triggers the current attribute updating.
return mixed the attribute value

Property Details

$attributes public_oe property

list of attributes that are to be automatically filled with the value specified via [[value]]. The array keys are the ActiveRecord events upon which the attributes are to be updated, 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. For example, php [ ActiveRecord::EVENT_BEFORE_INSERT => ['attribute1', 'attribute2'], ActiveRecord::EVENT_BEFORE_UPDATE => 'attribute2', ]
public $attributes

$skipUpdateOnClean public_oe property

whether to skip this behavior when the $owner has not been modified
Since: 2.0.8
public $skipUpdateOnClean

$value public_oe 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 [[\yii\db\Expression|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, php function ($event) { return value will be assigned to the attribute }
public $value