1 | <?php |
||
40 | class ExistValidator extends Validator |
||
41 | { |
||
42 | /** |
||
43 | * @var string the name of the ActiveRecord class that should be used to validate the existence |
||
44 | * of the current attribute value. It not set, it will use the ActiveRecord class of the attribute being validated. |
||
45 | * @see targetAttribute |
||
46 | */ |
||
47 | public $targetClass; |
||
48 | /** |
||
49 | * @var string|array the name of the ActiveRecord attribute that should be used to |
||
50 | * validate the existence of the current attribute value. If not set, it will use the name |
||
51 | * of the attribute currently being validated. You may use an array to validate the existence |
||
52 | * of multiple columns at the same time. The array key is the name of the attribute with the value to validate, |
||
53 | * the array value is the name of the database field to search. |
||
54 | */ |
||
55 | public $targetAttribute; |
||
56 | /** |
||
57 | * @var string|array|\Closure additional filter to be applied to the DB query used to check the existence of the attribute value. |
||
58 | * This can be a string or an array representing the additional query condition (refer to [[\yii\db\Query::where()]] |
||
59 | * on the format of query condition), or an anonymous function with the signature `function ($query)`, where `$query` |
||
60 | * is the [[\yii\db\Query|Query]] object that you can modify in the function. |
||
61 | */ |
||
62 | public $filter; |
||
63 | /** |
||
64 | * @var bool whether to allow array type attribute. |
||
65 | */ |
||
66 | public $allowArray = false; |
||
67 | |||
68 | /** |
||
69 | * @var string and|or define how target attributes are related |
||
70 | * @since 2.0.11 |
||
71 | */ |
||
72 | public $targetAttributeJunction = 'and'; |
||
73 | |||
74 | /** |
||
75 | * @inheritdoc |
||
76 | */ |
||
77 | 12 | public function init() |
|
84 | |||
85 | /** |
||
86 | * @inheritdoc |
||
87 | */ |
||
88 | 6 | public function validateAttribute($model, $attribute) |
|
118 | |||
119 | /** |
||
120 | * Processes attributes' relations described in $targetAttribute parameter into conditions, compatible with |
||
121 | * [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
122 | * |
||
123 | * @param $targetAttribute array|string $attribute the name of the ActiveRecord attribute that should be used to |
||
124 | * validate the existence of the current attribute value. If not set, it will use the name |
||
125 | * of the attribute currently being validated. You may use an array to validate the existence |
||
126 | * of multiple columns at the same time. The array key is the name of the attribute with the value to validate, |
||
127 | * the array value is the name of the database field to search. |
||
128 | * @param \yii\base\Model $model the data model to be validated |
||
129 | * @param string $attribute the name of the attribute to be validated in the $model |
||
130 | * @return array conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
131 | * @throws InvalidConfigException |
||
132 | */ |
||
133 | 6 | private function prepareConditions($targetAttribute, $model, $attribute) |
|
148 | |||
149 | /** |
||
150 | * @inheritdoc |
||
151 | */ |
||
152 | 6 | protected function validateValue($value) |
|
172 | |||
173 | /** |
||
174 | * Creates a query instance with the given condition. |
||
175 | * @param string $targetClass the target AR class |
||
176 | * @param mixed $condition query condition |
||
177 | * @return \yii\db\ActiveQueryInterface the query instance |
||
178 | */ |
||
179 | 9 | protected function createQuery($targetClass, $condition) |
|
191 | } |
||
192 |
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.