1 | <?php |
||
43 | class ExistValidator extends Validator |
||
44 | { |
||
45 | /** |
||
46 | * @var string the name of the ActiveRecord class that should be used to validate the existence |
||
47 | * of the current attribute value. If not set, it will use the ActiveRecord class of the attribute being validated. |
||
48 | * @see targetAttribute |
||
49 | */ |
||
50 | public $targetClass; |
||
51 | /** |
||
52 | * @var string|array the name of the ActiveRecord attribute that should be used to |
||
53 | * validate the existence of the current attribute value. If not set, it will use the name |
||
54 | * of the attribute currently being validated. You may use an array to validate the existence |
||
55 | * of multiple columns at the same time. The array key is the name of the attribute with the value to validate, |
||
56 | * the array value is the name of the database field to search. |
||
57 | */ |
||
58 | public $targetAttribute; |
||
59 | /** |
||
60 | * @var string|array|\Closure additional filter to be applied to the DB query used to check the existence of the attribute value. |
||
61 | * This can be a string or an array representing the additional query condition (refer to [[\yii\db\Query::where()]] |
||
62 | * on the format of query condition), or an anonymous function with the signature `function ($query)`, where `$query` |
||
63 | * is the [[\yii\db\Query|Query]] object that you can modify in the function. |
||
64 | */ |
||
65 | public $filter; |
||
66 | /** |
||
67 | * @var bool whether to allow array type attribute. |
||
68 | */ |
||
69 | public $allowArray = false; |
||
70 | /** |
||
71 | * @var string and|or define how target attributes are related |
||
72 | * @since 2.0.11 |
||
73 | */ |
||
74 | public $targetAttributeJunction = 'and'; |
||
75 | |||
76 | |||
77 | /** |
||
78 | * @inheritdoc |
||
79 | */ |
||
80 | 18 | public function init() |
|
87 | |||
88 | /** |
||
89 | * @inheritdoc |
||
90 | */ |
||
91 | 12 | public function validateAttribute($model, $attribute) |
|
121 | |||
122 | /** |
||
123 | * Processes attributes' relations described in $targetAttribute parameter into conditions, compatible with |
||
124 | * [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
125 | * |
||
126 | * @param $targetAttribute array|string $attribute the name of the ActiveRecord attribute that should be used to |
||
127 | * validate the existence of the current attribute value. If not set, it will use the name |
||
128 | * of the attribute currently being validated. You may use an array to validate the existence |
||
129 | * of multiple columns at the same time. The array key is the name of the attribute with the value to validate, |
||
130 | * the array value is the name of the database field to search. |
||
131 | * If the key and the value are the same, you can just specify the value. |
||
132 | * @param \yii\base\Model $model the data model to be validated |
||
133 | * @param string $attribute the name of the attribute to be validated in the $model |
||
134 | * @return array conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
135 | * @throws InvalidConfigException |
||
136 | */ |
||
137 | 12 | private function prepareConditions($targetAttribute, $model, $attribute) |
|
159 | |||
160 | /** |
||
161 | * @param Model $model the data model to be validated |
||
162 | * @return string Target class name |
||
163 | */ |
||
164 | 12 | private function getTargetClass($model) |
|
168 | |||
169 | /** |
||
170 | * @inheritdoc |
||
171 | */ |
||
172 | 6 | protected function validateValue($value) |
|
193 | |||
194 | /** |
||
195 | * Creates a query instance with the given condition. |
||
196 | * @param string $targetClass the target AR class |
||
197 | * @param mixed $condition query condition |
||
198 | * @return \yii\db\ActiveQueryInterface the query instance |
||
199 | */ |
||
200 | 15 | protected function createQuery($targetClass, $condition) |
|
212 | |||
213 | /** |
||
214 | * Returns conditions with alias. |
||
215 | * @param ActiveQuery $query |
||
216 | * @param array $conditions array of condition, keys to be modified |
||
217 | * @param null|string $alias set empty string for no apply alias. Set null for apply primary table alias |
||
218 | * @return array |
||
219 | */ |
||
220 | 12 | private function applyTableAlias($query, $conditions, $alias = null) |
|
242 | } |
||
243 |
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.