1 | <?php |
||
42 | class UniqueValidator extends Validator |
||
43 | { |
||
44 | /** |
||
45 | * @var string the name of the ActiveRecord class that should be used to validate the uniqueness |
||
46 | * of the current attribute value. |
||
47 | * This must be a fully qualified class name. |
||
48 | * |
||
49 | * If not set, it will use the ActiveRecord class of the attribute being validated. |
||
50 | * |
||
51 | * @see targetAttribute |
||
52 | */ |
||
53 | public $targetClass; |
||
54 | /** |
||
55 | * @var string|array the name of the [[\yii\db\ActiveRecord|ActiveRecord]] attribute that should be used to |
||
56 | * validate the uniqueness of the current attribute value. If not set, it will use the name |
||
57 | * of the attribute currently being validated. You may use an array to validate the uniqueness |
||
58 | * of multiple columns at the same time. The array values are the attributes that will be |
||
59 | * used to validate the uniqueness, while the array keys are the attributes whose values are to be validated. |
||
60 | */ |
||
61 | public $targetAttribute; |
||
62 | /** |
||
63 | * @var string|array|\Closure additional filter to be applied to the DB query used to check the uniqueness of the attribute value. |
||
64 | * This can be a string or an array representing the additional query condition (refer to [[\yii\db\Query::where()]] |
||
65 | * on the format of query condition), or an anonymous function with the signature `function ($query)`, where `$query` |
||
66 | * is the [[\yii\db\Query|Query]] object that you can modify in the function. |
||
67 | */ |
||
68 | public $filter; |
||
69 | /** |
||
70 | * @var string the user-defined error message. |
||
71 | * |
||
72 | * When validating single attribute, it may contain |
||
73 | * the following placeholders which will be replaced accordingly by the validator: |
||
74 | * |
||
75 | * - `{attribute}`: the label of the attribute being validated |
||
76 | * - `{value}`: the value of the attribute being validated |
||
77 | * |
||
78 | * When validating mutliple attributes, it may contain the following placeholders: |
||
79 | * |
||
80 | * - `{attributes}`: the labels of the attributes being validated. |
||
81 | * - `{values}`: the values of the attributes being validated. |
||
82 | */ |
||
83 | public $message; |
||
84 | /** |
||
85 | * @var string and|or define how target attributes are related |
||
86 | * @since 2.0.11 |
||
87 | */ |
||
88 | public $targetAttributeJunction = 'and'; |
||
89 | /** |
||
90 | * @var bool whether this validator is forced to always use master DB |
||
91 | * @since 2.0.14 |
||
92 | */ |
||
93 | public $forceMasterDb = true; |
||
94 | |||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | 67 | public function init() |
|
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | 52 | public function validateAttribute($model, $attribute) |
|
151 | |||
152 | /** |
||
153 | * @param Model $model the data model to be validated |
||
154 | * @return string Target class name |
||
155 | */ |
||
156 | 61 | private function getTargetClass($model) |
|
160 | |||
161 | /** |
||
162 | * Checks whether the $model exists in the database. |
||
163 | * |
||
164 | * @param string $targetClass the name of the ActiveRecord class that should be used to validate the uniqueness |
||
165 | * of the current attribute value. |
||
166 | * @param array $conditions conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
167 | * @param Model $model the data model to be validated |
||
168 | * |
||
169 | * @return bool whether the model already exists |
||
170 | */ |
||
171 | 49 | private function modelExists($targetClass, $conditions, $model) |
|
212 | |||
213 | /** |
||
214 | * Prepares a query by applying filtering conditions defined in $conditions method property |
||
215 | * and [[filter]] class property. |
||
216 | * |
||
217 | * @param ActiveRecordInterface $targetClass the name of the ActiveRecord class that should be used to validate |
||
218 | * the uniqueness of the current attribute value. |
||
219 | * @param array $conditions conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format |
||
220 | * |
||
221 | * @return ActiveQueryInterface|ActiveQuery |
||
222 | */ |
||
223 | 52 | private function prepareQuery($targetClass, $conditions) |
|
235 | |||
236 | /** |
||
237 | * Processes attributes' relations described in $targetAttribute parameter into conditions, compatible with |
||
238 | * [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
239 | * |
||
240 | * @param string|array $targetAttribute the name of the [[\yii\db\ActiveRecord|ActiveRecord]] attribute that |
||
241 | * should be used to validate the uniqueness of the current attribute value. You may use an array to validate |
||
242 | * the uniqueness of multiple columns at the same time. The array values are the attributes that will be |
||
243 | * used to validate the uniqueness, while the array keys are the attributes whose values are to be validated. |
||
244 | * If the key and the value are the same, you can just specify the value. |
||
245 | * @param Model $model the data model to be validated |
||
246 | * @param string $attribute the name of the attribute to be validated in the $model |
||
247 | * |
||
248 | * @return array conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
249 | */ |
||
250 | 55 | private function prepareConditions($targetAttribute, $model, $attribute) |
|
269 | |||
270 | /** |
||
271 | * Builds and adds error message to the specified model attribute. |
||
272 | * Should be used when [[targetAttribute]] is an array (is a combination of attributes). |
||
273 | * @param \yii\base\Model $model the data model. |
||
274 | * @param string $attribute the name of the attribute. |
||
275 | */ |
||
276 | 6 | private function addComboNotUniqueError($model, $attribute) |
|
294 | |||
295 | /** |
||
296 | * Returns conditions with alias. |
||
297 | * @param ActiveQuery $query |
||
298 | * @param array $conditions array of condition, keys to be modified |
||
299 | * @param null|string $alias set empty string for no apply alias. Set null for apply primary table alias |
||
300 | * @return array |
||
301 | */ |
||
302 | 55 | private function applyTableAlias($query, $conditions, $alias = null) |
|
324 | } |
||
325 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: