1 | <?php |
||
42 | class ExistValidator extends Validator |
||
43 | { |
||
44 | /** |
||
45 | * @var string the name of the ActiveRecord class that should be used to validate the existence |
||
46 | * of the current attribute value. If not set, it will use the ActiveRecord class of the attribute being validated. |
||
47 | * @see targetAttribute |
||
48 | */ |
||
49 | public $targetClass; |
||
50 | /** |
||
51 | * @var string|array the name of the ActiveRecord attribute that should be used to |
||
52 | * validate the existence of the current attribute value. If not set, it will use the name |
||
53 | * of the attribute currently being validated. You may use an array to validate the existence |
||
54 | * of multiple columns at the same time. The array key is the name of the attribute with the value to validate, |
||
55 | * the array value is the name of the database field to search. |
||
56 | */ |
||
57 | public $targetAttribute; |
||
58 | /** |
||
59 | * @var string|array|\Closure additional filter to be applied to the DB query used to check the existence of the attribute value. |
||
60 | * This can be a string or an array representing the additional query condition (refer to [[\yii\db\Query::where()]] |
||
61 | * on the format of query condition), or an anonymous function with the signature `function ($query)`, where `$query` |
||
62 | * is the [[\yii\db\Query|Query]] object that you can modify in the function. |
||
63 | */ |
||
64 | public $filter; |
||
65 | /** |
||
66 | * @var bool whether to allow array type attribute. |
||
67 | */ |
||
68 | public $allowArray = false; |
||
69 | /** |
||
70 | * @var string and|or define how target attributes are related |
||
71 | * @since 2.0.11 |
||
72 | */ |
||
73 | public $targetAttributeJunction = 'and'; |
||
74 | |||
75 | |||
76 | /** |
||
77 | * @inheritdoc |
||
78 | */ |
||
79 | 12 | public function init() |
|
86 | |||
87 | /** |
||
88 | * @inheritdoc |
||
89 | */ |
||
90 | 6 | public function validateAttribute($model, $attribute) |
|
120 | |||
121 | /** |
||
122 | * Processes attributes' relations described in $targetAttribute parameter into conditions, compatible with |
||
123 | * [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
124 | * |
||
125 | * @param $targetAttribute array|string $attribute the name of the ActiveRecord attribute that should be used to |
||
126 | * validate the existence of the current attribute value. If not set, it will use the name |
||
127 | * of the attribute currently being validated. You may use an array to validate the existence |
||
128 | * of multiple columns at the same time. The array key is the name of the attribute with the value to validate, |
||
129 | * the array value is the name of the database field to search. |
||
130 | * If the key and the value are the same, you can just specify the value. |
||
131 | * @param \yii\base\Model $model the data model to be validated |
||
132 | * @param string $attribute the name of the attribute to be validated in the $model |
||
133 | * @return array conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
134 | * @throws InvalidConfigException |
||
135 | */ |
||
136 | 6 | private function prepareConditions($targetAttribute, $model, $attribute) |
|
156 | |||
157 | /** |
||
158 | * @param Model $model the data model to be validated |
||
159 | * @return string Target class name |
||
160 | */ |
||
161 | 6 | private function getTargetClass($model) |
|
165 | |||
166 | /** |
||
167 | * @inheritdoc |
||
168 | */ |
||
169 | 6 | protected function validateValue($value) |
|
189 | |||
190 | /** |
||
191 | * Creates a query instance with the given condition. |
||
192 | * @param string $targetClass the target AR class |
||
193 | * @param mixed $condition query condition |
||
194 | * @return \yii\db\ActiveQueryInterface the query instance |
||
195 | */ |
||
196 | 9 | protected function createQuery($targetClass, $condition) |
|
208 | |||
209 | /** |
||
210 | * Prefix conditions with aliases |
||
211 | * |
||
212 | * @param ActiveRecord $model |
||
213 | * @param array $conditions |
||
214 | * @return array |
||
215 | */ |
||
216 | 6 | private function prefixConditions($model, $conditions) |
|
232 | } |
||
233 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.