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::class, |
||
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 | 35 | public function events() |
|
99 | { |
||
100 | 35 | return array_fill_keys( |
|
101 | 35 | array_keys($this->attributes), |
|
102 | 35 | 'evaluateAttributes' |
|
103 | ); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Evaluates the attribute value and assigns it to the current attributes. |
||
108 | * @param Event $event |
||
109 | */ |
||
110 | 37 | public function evaluateAttributes($event) |
|
111 | { |
||
112 | 37 | if ($this->skipUpdateOnClean |
|
113 | 37 | && $event->name == ActiveRecord::EVENT_BEFORE_UPDATE |
|
114 | 37 | && empty($this->owner->dirtyAttributes) |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
115 | ) { |
||
116 | 1 | return; |
|
117 | } |
||
118 | |||
119 | 37 | if (!empty($this->attributes[$event->name])) { |
|
120 | 37 | $attributes = (array) $this->attributes[$event->name]; |
|
121 | 37 | $value = $this->getValue($event); |
|
122 | 37 | foreach ($attributes as $attribute) { |
|
123 | // ignore attribute names which are not string (e.g. when set by TimestampBehavior::updatedAtAttribute) |
||
124 | 37 | if (is_string($attribute)) { |
|
125 | 37 | if ($this->preserveNonEmptyValues && !empty($this->owner->$attribute)) { |
|
126 | 1 | continue; |
|
127 | } |
||
128 | 36 | $this->owner->$attribute = $value; |
|
129 | } |
||
130 | } |
||
131 | } |
||
132 | 37 | } |
|
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 |