1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\behaviors; |
9
|
|
|
|
10
|
|
|
use Closure; |
11
|
|
|
use yii\base\Behavior; |
12
|
|
|
use yii\base\Event; |
13
|
|
|
use yii\db\ActiveRecord; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* AttributeBehavior automatically assigns a specified value to one or multiple attributes of an ActiveRecord |
17
|
|
|
* object when certain events happen. |
18
|
|
|
* |
19
|
|
|
* To use AttributeBehavior, configure the [[attributes]] property which should specify the list of attributes |
20
|
|
|
* that need to be updated and the corresponding events that should trigger the update. Then configure the |
21
|
|
|
* [[value]] property with a PHP callable whose return value will be used to assign to the current attribute(s). |
22
|
|
|
* For example, |
23
|
|
|
* |
24
|
|
|
* ```php |
25
|
|
|
* use yii\behaviors\AttributeBehavior; |
26
|
|
|
* |
27
|
|
|
* public function behaviors() |
28
|
|
|
* { |
29
|
|
|
* return [ |
30
|
|
|
* [ |
31
|
|
|
* 'class' => AttributeBehavior::className(), |
32
|
|
|
* 'attributes' => [ |
33
|
|
|
* ActiveRecord::EVENT_BEFORE_INSERT => 'attribute1', |
34
|
|
|
* ActiveRecord::EVENT_BEFORE_UPDATE => 'attribute2', |
35
|
|
|
* ], |
36
|
|
|
* 'value' => function ($event) { |
37
|
|
|
* return 'some value'; |
38
|
|
|
* }, |
39
|
|
|
* ], |
40
|
|
|
* ]; |
41
|
|
|
* } |
42
|
|
|
* ``` |
43
|
|
|
* |
44
|
|
|
* Because attribute values will be set automatically by this behavior, they are usually not user input and should therefore |
45
|
|
|
* not be validated, i.e. they should not appear in the [[\yii\base\Model::rules()|rules()]] method of the model. |
46
|
|
|
* |
47
|
|
|
* @author Luciano Baraglia <[email protected]> |
48
|
|
|
* @author Qiang Xue <[email protected]> |
49
|
|
|
* @since 2.0 |
50
|
|
|
*/ |
51
|
|
|
class AttributeBehavior extends Behavior |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* @var array list of attributes that are to be automatically filled with the value specified via [[value]]. |
55
|
|
|
* The array keys are the ActiveRecord events upon which the attributes are to be updated, |
56
|
|
|
* and the array values are the corresponding attribute(s) to be updated. You can use a string to represent |
57
|
|
|
* a single attribute, or an array to represent a list of attributes. For example, |
58
|
|
|
* |
59
|
|
|
* ```php |
60
|
|
|
* [ |
61
|
|
|
* ActiveRecord::EVENT_BEFORE_INSERT => ['attribute1', 'attribute2'], |
62
|
|
|
* ActiveRecord::EVENT_BEFORE_UPDATE => 'attribute2', |
63
|
|
|
* ] |
64
|
|
|
* ``` |
65
|
|
|
*/ |
66
|
|
|
public $attributes = []; |
67
|
|
|
/** |
68
|
|
|
* @var mixed the value that will be assigned to the current attributes. This can be an anonymous function, |
69
|
|
|
* callable in array format (e.g. `[$this, 'methodName']`), an [[\yii\db\Expression|Expression]] object representing a DB expression |
70
|
|
|
* (e.g. `new Expression('NOW()')`), scalar, string or an arbitrary value. If the former, the return value of the |
71
|
|
|
* function will be assigned to the attributes. |
72
|
|
|
* The signature of the function should be as follows, |
73
|
|
|
* |
74
|
|
|
* ```php |
75
|
|
|
* function ($event) |
76
|
|
|
* { |
77
|
|
|
* // return value will be assigned to the attribute |
78
|
|
|
* } |
79
|
|
|
* ``` |
80
|
|
|
*/ |
81
|
|
|
public $value; |
82
|
|
|
/** |
83
|
|
|
* @var bool whether to skip this behavior when the `$owner` has not been |
84
|
|
|
* modified |
85
|
|
|
* @since 2.0.8 |
86
|
|
|
*/ |
87
|
|
|
public $skipUpdateOnClean = true; |
88
|
|
|
/** |
89
|
|
|
* @var bool whether to preserve non-empty attribute values. |
90
|
|
|
* @since 2.0.13 |
91
|
|
|
*/ |
92
|
|
|
public $preserveNonEmptyValues = false; |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritdoc |
97
|
|
|
*/ |
98
|
30 |
|
public function events() |
99
|
|
|
{ |
100
|
30 |
|
return array_fill_keys( |
101
|
30 |
|
array_keys($this->attributes), |
102
|
30 |
|
'evaluateAttributes' |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Evaluates the attribute value and assigns it to the current attributes. |
108
|
|
|
* @param Event $event |
109
|
|
|
*/ |
110
|
29 |
|
public function evaluateAttributes($event) |
111
|
|
|
{ |
112
|
29 |
|
if ($this->skipUpdateOnClean |
113
|
29 |
|
&& $event->name == ActiveRecord::EVENT_BEFORE_UPDATE |
114
|
4 |
|
&& empty($this->owner->dirtyAttributes) |
|
|
|
|
115
|
|
|
) { |
116
|
1 |
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
29 |
|
if (!empty($this->attributes[$event->name])) { |
120
|
29 |
|
$attributes = (array) $this->attributes[$event->name]; |
121
|
29 |
|
$value = $this->getValue($event); |
122
|
29 |
|
foreach ($attributes as $attribute) { |
123
|
|
|
// ignore attribute names which are not string (e.g. when set by TimestampBehavior::updatedAtAttribute) |
124
|
29 |
|
if (is_string($attribute)) { |
125
|
29 |
|
if ($this->preserveNonEmptyValues && !empty($this->owner->$attribute)) { |
126
|
1 |
|
continue; |
127
|
|
|
} |
128
|
28 |
|
$this->owner->$attribute = $value; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
29 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns the value for the current attributes. |
136
|
|
|
* This method is called by [[evaluateAttributes()]]. Its return value will be assigned |
137
|
|
|
* to the attributes corresponding to the triggering event. |
138
|
|
|
* @param Event $event the event that triggers the current attribute updating. |
139
|
|
|
* @return mixed the attribute value |
140
|
|
|
*/ |
141
|
14 |
|
protected function getValue($event) |
142
|
|
|
{ |
143
|
14 |
|
if ($this->value instanceof Closure || is_array($this->value) && is_callable($this->value)) { |
144
|
8 |
|
return call_user_func($this->value, $event); |
145
|
|
|
} |
146
|
|
|
|
147
|
6 |
|
return $this->value; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.