Class yii\behaviors\AttributesBehavior
AttributesBehavior 是在某个事件发生的时候, 用来给 ActiveRecord 对象的一个或多个属性自动设置指定值的行为。
要使用 AttributesBehavior,就要配置 $attributes 属性,它指明了需要更新的属性列表和 触发这个更新操作对应的事件。然后再配置短数组的值为一个 PHP 匿名函数, 该函数应该返回设置给当前属性的值。 比如,
use yii\behaviors\AttributesBehavior;
public function behaviors()
{
return [
[
'class' => AttributesBehavior::className(),
'attributes' => [
'attribute1' => [
ActiveRecord::EVENT_BEFORE_INSERT => new Expression('NOW()'),
ActiveRecord::EVENT_BEFORE_UPDATE => \Yii::$app->formatter->asDatetime('2017-07-13'),
],
'attribute2' => [
ActiveRecord::EVENT_BEFORE_VALIDATE => [$this, 'storeAttributes'],
ActiveRecord::EVENT_AFTER_VALIDATE => [$this, 'restoreAttributes'],
],
'attribute3' => [
ActiveRecord::EVENT_BEFORE_VALIDATE => $fn2 = [$this, 'getAttribute2'],
ActiveRecord::EVENT_AFTER_VALIDATE => $fn2,
],
'attribute4' => [
ActiveRecord::EVENT_BEFORE_DELETE => function ($event, $attribute) {
static::disabled() || $event->isValid = false;
},
],
],
],
];
}
由于属性值会被这个行为自动设置,所以属性值不必用户输入也因此没有必要验证。 因此,这些属性不应该出现在 rules() 这个模型方法中。
公共属性
| 属性 | 类型 | 描述 | 被定义在 |
|---|---|---|---|
| $attributes | array | 指出将被自动更新的属性列表,而被更新的值通过短数组给出。
数组的键就是更新于事件之上的 ActiveRecord 对象的属性,
而数组的值则是对应事件的短数组。对这样的短数组而言:
数组的键就是 ActiveRecord 事件,属性就是根据这些事件更新的,
数组的值则是要设置给当前属性的值。数组的值可以是一个匿名函数,
数组格式的回调方法(比如 [$this, 'methodName']),一个表示 DB 表达式的 Expression 对象
(比如 new Expression('NOW()')),标量,字符串或者一个任意的值。如果是前者,
那么函数的返回值将设置给这个属性
`php
[
'attribute1' => [
ActiveRecord::EVENT_BEFORE_INSERT => new Expression('NOW()'),
ActiveRecord::EVENT_BEFORE_UPDATE => \Yii::$app->formatter->asDatetime('2017-07-13'),
],
'attribute2' => [
ActiveRecord::EVENT_BEFORE_VALIDATE => [$this, 'storeAttributes'],
ActiveRecord::EVENT_AFTER_VALIDATE => [$this, 'restoreAttributes'],
],
'attribute3' => [
ActiveRecord::EVENT_BEFORE_VALIDATE => $fn2 = [$this, 'getAttribute2'],
ActiveRecord::EVENT_AFTER_VALIDATE => $fn2,
],
'attribute4' => [
ActiveRecord::EVENT_BEFORE_DELETE => function ($event, $attribute) {
static::disabled() || $event->isValid = false;
},
],
]
` |
yii\behaviors\AttributesBehavior |
| $order | array | 用事件给出将被更新的属性的顺序列表。
数组的键就是属性据此完成更新的 ActiveRecord 事件,
而数组的值则是对应属性的顺序。
不在这些数组里的属性将会在最后处理。
如果 $attributes 里的属性没有指明事件,那么这些属性会被忽略不被更新。
`php
[
ActiveRecord::EVENT_BEFORE_VALIDATE => ['attribute1', 'attribute2'],
ActiveRecord::EVENT_AFTER_VALIDATE => ['attribute2', 'attribute1'],
]
` |
yii\behaviors\AttributesBehavior |
| $owner | yii\base\Component|null | The owner of this behavior | yii\base\Behavior |
| $preserveNonEmptyValues | boolean | 是否保留非空的值不更新。 | yii\behaviors\AttributesBehavior |
| $skipUpdateOnClean | boolean | 当 $owner 没被更新时是否跳过该行为。 |
yii\behaviors\AttributesBehavior |
公共方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| __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() | 解析属性的值并更新到当前属性上。 | yii\behaviors\AttributesBehavior |
| events() | Declares event handlers for the $owner's events. | yii\behaviors\AttributesBehavior |
| 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\base\BaseObject |
受保护的方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| getValue() | 返回当前属性的值。 该方法是由 evaluateAttributes() 内部调用的。 它的返回值将会根据触发的事件设置到目标属性上。 | yii\behaviors\AttributesBehavior |
属性详情
指出将被自动更新的属性列表,而被更新的值通过短数组给出。
数组的键就是更新于事件之上的 ActiveRecord 对象的属性,
而数组的值则是对应事件的短数组。对这样的短数组而言:
数组的键就是 ActiveRecord 事件,属性就是根据这些事件更新的,
数组的值则是要设置给当前属性的值。数组的值可以是一个匿名函数,
数组格式的回调方法(比如 [$this, 'methodName']),一个表示 DB 表达式的 Expression 对象
(比如 new Expression('NOW()')),标量,字符串或者一个任意的值。如果是前者,
那么函数的返回值将设置给这个属性
[
'attribute1' => [
ActiveRecord::EVENT_BEFORE_INSERT => new Expression('NOW()'),
ActiveRecord::EVENT_BEFORE_UPDATE => \Yii::$app->formatter->asDatetime('2017-07-13'),
],
'attribute2' => [
ActiveRecord::EVENT_BEFORE_VALIDATE => [$this, 'storeAttributes'],
ActiveRecord::EVENT_AFTER_VALIDATE => [$this, 'restoreAttributes'],
],
'attribute3' => [
ActiveRecord::EVENT_BEFORE_VALIDATE => $fn2 = [$this, 'getAttribute2'],
ActiveRecord::EVENT_AFTER_VALIDATE => $fn2,
],
'attribute4' => [
ActiveRecord::EVENT_BEFORE_DELETE => function ($event, $attribute) {
static::disabled() || $event->isValid = false;
},
],
]
用事件给出将被更新的属性的顺序列表。 数组的键就是属性据此完成更新的 ActiveRecord 事件, 而数组的值则是对应属性的顺序。 不在这些数组里的属性将会在最后处理。 如果 $attributes 里的属性没有指明事件,那么这些属性会被忽略不被更新。
[
ActiveRecord::EVENT_BEFORE_VALIDATE => ['attribute1', 'attribute2'],
ActiveRecord::EVENT_AFTER_VALIDATE => ['attribute2', 'attribute1'],
]
是否保留非空的值不更新。
当 $owner 没被更新时是否跳过该行为。
方法详情
解析属性的值并更新到当前属性上。
| public void evaluateAttributes($event) | ||
| $event | yii\base\Event | |
Declares event handlers for the $owner's events.
Child classes may override this method to declare what PHP callbacks should be attached to the events of the $owner component.
The callbacks will be attached to the $owner's events when the behavior is attached to the owner; and they will be detached from the events when the behavior is detached from the component.
The callbacks can be any of the following:
- method in this behavior:
'handleClick', equivalent to[$this, 'handleClick'] - object method:
[$object, 'handleClick'] - static method:
['Page', 'handleClick'] - anonymous function:
function ($event) { ... }
The following is an example:
[
Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate',
Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',
]
| public array events() | ||
| return | array | Events (array keys) and the corresponding event handler methods (array values). |
|---|---|---|
返回当前属性的值。 该方法是由 evaluateAttributes() 内部调用的。 它的返回值将会根据触发的事件设置到目标属性上。
| protected mixed getValue($attribute, $event) | ||
| $attribute | string | 目标属性名 |
| $event | yii\base\Event | 触发当前属性开始更新动作的事件 |
| return | mixed | 属性值 |
|---|---|---|