PHP Класс 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.
Показать файл
Открыть проект
Примеры использования класса
Открытые свойства
Свойство |
Тип |
Описание |
|
$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
}
|
|
Открытые методы
Метод |
Описание |
|
evaluateAttributes ( Event $event ) |
Evaluates the attribute value and assigns it to the current attributes. |
|
events ( ) |
|
|
Защищенные методы
Метод |
Описание |
|
getValue ( Event $event ) : mixed |
Returns the value for the current attributes. |
|
Описание методов
evaluateAttributes()
публичный Метод
Evaluates the attribute value and assigns it to the current attributes.
getValue()
защищенный Метод
protected getValue ( Event $event ) : mixed |
$event |
yii\base\Event |
the event that triggers the current attribute updating. |
Результат |
mixed |
the attribute value |
Описание свойств
$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
public $skipUpdateOnClean |
$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
}