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.
파일 보기
프로젝트 열기: yiisoft/yii2
1 사용 예제들
공개 프로퍼티들
프로퍼티 |
타입 |
설명 |
|
$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.
protected getValue ( Event $event ) : mixed |
$event |
yii\base\Event |
the event that triggers the current attribute updating. |
리턴 |
mixed |
the attribute value |
프로퍼티 상세
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 |
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
}