Complex classes like ActiveQuery often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ActiveQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
73 | class ActiveQuery extends Query implements ActiveQueryInterface |
||
74 | { |
||
75 | use ActiveQueryTrait; |
||
76 | use ActiveRelationTrait; |
||
77 | |||
78 | /** |
||
79 | * @event Event an event that is triggered when the query is initialized via [[init()]]. |
||
80 | */ |
||
81 | const EVENT_INIT = 'init'; |
||
82 | |||
83 | /** |
||
84 | * @var string the SQL statement to be executed for retrieving AR records. |
||
85 | * This is set by [[ActiveRecord::findBySql()]]. |
||
86 | */ |
||
87 | public $sql; |
||
88 | /** |
||
89 | * @var string|array the join condition to be used when this query is used in a relational context. |
||
90 | * The condition will be used in the ON part when [[ActiveQuery::joinWith()]] is called. |
||
91 | * Otherwise, the condition will be used in the WHERE part of a query. |
||
92 | * Please refer to [[Query::where()]] on how to specify this parameter. |
||
93 | * @see onCondition() |
||
94 | */ |
||
95 | public $on; |
||
96 | /** |
||
97 | * @var array a list of relations that this query should be joined with |
||
98 | */ |
||
99 | public $joinWith; |
||
100 | |||
101 | |||
102 | /** |
||
103 | * Constructor. |
||
104 | * @param string $modelClass the model class associated with this query |
||
105 | * @param array $config configurations to be applied to the newly created query object |
||
106 | */ |
||
107 | 396 | public function __construct($modelClass, $config = []) |
|
112 | |||
113 | /** |
||
114 | * Initializes the object. |
||
115 | * This method is called at the end of the constructor. The default implementation will trigger |
||
116 | * an [[EVENT_INIT]] event. If you override this method, make sure you call the parent implementation at the end |
||
117 | * to ensure triggering of the event. |
||
118 | */ |
||
119 | 396 | public function init() |
|
124 | |||
125 | /** |
||
126 | * Executes query and returns all results as an array. |
||
127 | * @param Connection $db the DB connection used to create the DB command. |
||
128 | * If null, the DB connection returned by [[modelClass]] will be used. |
||
129 | * @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned. |
||
130 | */ |
||
131 | 173 | public function all($db = null) |
|
135 | |||
136 | /** |
||
137 | * @inheritdoc |
||
138 | */ |
||
139 | 316 | public function prepare($builder) |
|
140 | { |
||
141 | // NOTE: because the same ActiveQuery may be used to build different SQL statements |
||
142 | // (e.g. by ActiveDataProvider, one for count query, the other for row data query, |
||
143 | // it is important to make sure the same ActiveQuery can be used to build SQL statements |
||
144 | // multiple times. |
||
145 | 316 | if (!empty($this->joinWith)) { |
|
146 | 36 | $this->buildJoinWith(); |
|
147 | 36 | $this->joinWith = null; // clean it up to avoid issue https://github.com/yiisoft/yii2/issues/2687 |
|
|
|||
148 | } |
||
149 | |||
150 | 316 | if (empty($this->from)) { |
|
151 | 316 | $this->from = [$this->getPrimaryTableName()]; |
|
152 | } |
||
153 | |||
154 | 316 | if (empty($this->select) && !empty($this->join)) { |
|
155 | 33 | list(, $alias) = $this->getTableNameAndAlias(); |
|
156 | 33 | $this->select = ["$alias.*"]; |
|
157 | } |
||
158 | |||
159 | 316 | if ($this->primaryModel === null) { |
|
160 | // eager loading |
||
161 | 315 | $query = Query::create($this); |
|
162 | } else { |
||
163 | // lazy loading of a relation |
||
164 | 82 | $where = $this->where; |
|
165 | |||
166 | 82 | if ($this->via instanceof self) { |
|
167 | // via junction table |
||
168 | 15 | $viaModels = $this->via->findJunctionRows([$this->primaryModel]); |
|
169 | 15 | $this->filterByModels($viaModels); |
|
170 | 73 | } elseif (is_array($this->via)) { |
|
171 | // via relation |
||
172 | /* @var $viaQuery ActiveQuery */ |
||
173 | 27 | list($viaName, $viaQuery) = $this->via; |
|
174 | 27 | if ($viaQuery->multiple) { |
|
175 | 27 | $viaModels = $viaQuery->all(); |
|
176 | 27 | $this->primaryModel->populateRelation($viaName, $viaModels); |
|
177 | } else { |
||
178 | $model = $viaQuery->one(); |
||
179 | $this->primaryModel->populateRelation($viaName, $model); |
||
180 | $viaModels = $model === null ? [] : [$model]; |
||
181 | } |
||
182 | 27 | $this->filterByModels($viaModels); |
|
183 | } else { |
||
184 | 73 | $this->filterByModels([$this->primaryModel]); |
|
185 | } |
||
186 | |||
187 | 82 | $query = Query::create($this); |
|
188 | 82 | $this->where = $where; |
|
189 | } |
||
190 | |||
191 | 316 | if (!empty($this->on)) { |
|
192 | 18 | $query->andWhere($this->on); |
|
193 | } |
||
194 | |||
195 | 316 | return $query; |
|
196 | } |
||
197 | |||
198 | /** |
||
199 | * @inheritdoc |
||
200 | */ |
||
201 | 269 | public function populate($rows) |
|
202 | { |
||
203 | 269 | if (empty($rows)) { |
|
204 | 49 | return []; |
|
205 | } |
||
206 | |||
207 | 263 | $models = $this->createModels($rows); |
|
208 | 263 | if (!empty($this->join) && $this->indexBy === null) { |
|
209 | 24 | $models = $this->removeDuplicatedModels($models); |
|
210 | } |
||
211 | 263 | if (!empty($this->with)) { |
|
212 | 69 | $this->findWith($this->with, $models); |
|
213 | } |
||
214 | |||
215 | 263 | if ($this->inverseOf !== null) { |
|
216 | 6 | $this->addInverseRelations($models); |
|
217 | } |
||
218 | |||
219 | 263 | if (!$this->asArray) { |
|
220 | 257 | foreach ($models as $model) { |
|
221 | 257 | $model->afterFind(); |
|
222 | } |
||
223 | } |
||
224 | |||
225 | 263 | return $models; |
|
226 | } |
||
227 | |||
228 | /** |
||
229 | * Removes duplicated models by checking their primary key values. |
||
230 | * This method is mainly called when a join query is performed, which may cause duplicated rows being returned. |
||
231 | * @param array $models the models to be checked |
||
232 | * @throws InvalidConfigException if model primary key is empty |
||
233 | * @return array the distinctive models |
||
234 | */ |
||
235 | 24 | private function removeDuplicatedModels($models) |
|
236 | { |
||
237 | 24 | $hash = []; |
|
238 | /* @var $class ActiveRecord */ |
||
239 | 24 | $class = $this->modelClass; |
|
240 | 24 | $pks = $class::primaryKey(); |
|
241 | |||
242 | 24 | if (count($pks) > 1) { |
|
243 | // composite primary key |
||
244 | 6 | foreach ($models as $i => $model) { |
|
245 | 6 | $key = []; |
|
246 | 6 | foreach ($pks as $pk) { |
|
247 | 6 | if (!isset($model[$pk])) { |
|
248 | // do not continue if the primary key is not part of the result set |
||
249 | 3 | break 2; |
|
250 | } |
||
251 | 6 | $key[] = $model[$pk]; |
|
252 | } |
||
253 | 3 | $key = serialize($key); |
|
254 | 3 | if (isset($hash[$key])) { |
|
255 | unset($models[$i]); |
||
256 | } else { |
||
257 | 6 | $hash[$key] = true; |
|
258 | } |
||
259 | } |
||
260 | } elseif (empty($pks)) { |
||
261 | throw new InvalidConfigException("Primary key of '{$class}' can not be empty."); |
||
262 | } else { |
||
263 | // single column primary key |
||
264 | 21 | $pk = reset($pks); |
|
265 | 21 | foreach ($models as $i => $model) { |
|
266 | 21 | if (!isset($model[$pk])) { |
|
267 | // do not continue if the primary key is not part of the result set |
||
268 | 3 | break; |
|
269 | } |
||
270 | 18 | $key = $model[$pk]; |
|
271 | 18 | if (isset($hash[$key])) { |
|
272 | 12 | unset($models[$i]); |
|
273 | 18 | } elseif ($key !== null) { |
|
274 | 18 | $hash[$key] = true; |
|
275 | } |
||
276 | } |
||
277 | } |
||
278 | |||
279 | 24 | return array_values($models); |
|
280 | } |
||
281 | |||
282 | /** |
||
283 | * Executes query and returns a single row of result. |
||
284 | * @param Connection|null $db the DB connection used to create the DB command. |
||
285 | * If `null`, the DB connection returned by [[modelClass]] will be used. |
||
286 | * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]], |
||
287 | * the query result may be either an array or an ActiveRecord object. `null` will be returned |
||
288 | * if the query results in nothing. |
||
289 | */ |
||
290 | 223 | public function one($db = null) |
|
291 | { |
||
292 | 223 | $row = parent::one($db); |
|
293 | 223 | if ($row !== false) { |
|
294 | 220 | $models = $this->populate([$row]); |
|
295 | 220 | return reset($models) ?: null; |
|
296 | } else { |
||
297 | 24 | return null; |
|
298 | } |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Creates a DB command that can be used to execute this query. |
||
303 | * @param Connection|null $db the DB connection used to create the DB command. |
||
304 | * If `null`, the DB connection returned by [[modelClass]] will be used. |
||
305 | * @return Command the created DB command instance. |
||
306 | */ |
||
307 | 316 | public function createCommand($db = null) |
|
308 | { |
||
309 | /* @var $modelClass ActiveRecord */ |
||
310 | 316 | $modelClass = $this->modelClass; |
|
311 | 316 | if ($db === null) { |
|
312 | 305 | $db = $modelClass::getDb(); |
|
313 | } |
||
314 | |||
315 | 316 | if ($this->sql === null) { |
|
316 | 313 | list ($sql, $params) = $db->getQueryBuilder()->build($this); |
|
317 | } else { |
||
318 | 3 | $sql = $this->sql; |
|
319 | 3 | $params = $this->params; |
|
320 | } |
||
321 | |||
322 | 316 | return $db->createCommand($sql, $params); |
|
323 | } |
||
324 | |||
325 | /** |
||
326 | * @inheritdoc |
||
327 | */ |
||
328 | 64 | protected function queryScalar($selectExpression, $db) |
|
329 | { |
||
330 | /* @var $modelClass ActiveRecord */ |
||
331 | 64 | $modelClass = $this->modelClass; |
|
332 | 64 | if ($db === null) { |
|
333 | 63 | $db = $modelClass::getDb(); |
|
334 | } |
||
335 | |||
336 | 64 | if ($this->sql === null) { |
|
337 | 61 | return parent::queryScalar($selectExpression, $db); |
|
338 | } |
||
339 | |||
340 | 3 | return (new Query)->select([$selectExpression]) |
|
341 | 3 | ->from(['c' => "({$this->sql})"]) |
|
342 | 3 | ->params($this->params) |
|
343 | 3 | ->createCommand($db) |
|
344 | 3 | ->queryScalar(); |
|
345 | } |
||
346 | |||
347 | /** |
||
348 | * Joins with the specified relations. |
||
349 | * |
||
350 | * This method allows you to reuse existing relation definitions to perform JOIN queries. |
||
351 | * Based on the definition of the specified relation(s), the method will append one or multiple |
||
352 | * JOIN statements to the current query. |
||
353 | * |
||
354 | * If the `$eagerLoading` parameter is true, the method will also perform eager loading for the specified relations, |
||
355 | * which is equivalent to calling [[with()]] using the specified relations. |
||
356 | * |
||
357 | * Note that because a JOIN query will be performed, you are responsible to disambiguate column names. |
||
358 | * |
||
359 | * This method differs from [[with()]] in that it will build up and execute a JOIN SQL statement |
||
360 | * for the primary table. And when `$eagerLoading` is true, it will call [[with()]] in addition with the specified relations. |
||
361 | * |
||
362 | * @param string|array $with the relations to be joined. This can either be a string, representing a relation name or |
||
363 | * an array with the following semantics: |
||
364 | * |
||
365 | * - Each array element represents a single relation. |
||
366 | * - You may specify the relation name as the array key and provide an anonymous functions that |
||
367 | * can be used to modify the relation queries on-the-fly as the array value. |
||
368 | * - If a relation query does not need modification, you may use the relation name as the array value. |
||
369 | * |
||
370 | * The relation name may optionally contain an alias for the relation table (e.g. `books b`). |
||
371 | * |
||
372 | * Sub-relations can also be specified, see [[with()]] for the syntax. |
||
373 | * |
||
374 | * In the following you find some examples: |
||
375 | * |
||
376 | * ```php |
||
377 | * // find all orders that contain books, and eager loading "books" |
||
378 | * Order::find()->joinWith('books', true, 'INNER JOIN')->all(); |
||
379 | * // find all orders, eager loading "books", and sort the orders and books by the book names. |
||
380 | * Order::find()->joinWith([ |
||
381 | * 'books' => function (\yii\db\ActiveQuery $query) { |
||
382 | * $query->orderBy('item.name'); |
||
383 | * } |
||
384 | * ])->all(); |
||
385 | * // find all orders that contain books of the category 'Science fiction', using the alias "b" for the books table |
||
386 | * Order::find()->joinWith(['books b'], true, 'INNER JOIN')->where(['b.category' => 'Science fiction'])->all(); |
||
387 | * ``` |
||
388 | * |
||
389 | * The alias syntax is available since version 2.0.7. |
||
390 | * |
||
391 | * @param bool|array $eagerLoading whether to eager load the relations specified in `$with`. |
||
392 | * When this is a boolean, it applies to all relations specified in `$with`. Use an array |
||
393 | * to explicitly list which relations in `$with` need to be eagerly loaded. Defaults to `true`. |
||
394 | * @param string|array $joinType the join type of the relations specified in `$with`. |
||
395 | * When this is a string, it applies to all relations specified in `$with`. Use an array |
||
396 | * in the format of `relationName => joinType` to specify different join types for different relations. |
||
397 | * @return $this the query object itself |
||
398 | */ |
||
399 | 42 | public function joinWith($with, $eagerLoading = true, $joinType = 'LEFT JOIN') |
|
400 | { |
||
401 | 42 | $relations = []; |
|
402 | 42 | foreach ((array) $with as $name => $callback) { |
|
403 | 42 | if (is_int($name)) { |
|
404 | 42 | $name = $callback; |
|
405 | 42 | $callback = null; |
|
406 | } |
||
407 | |||
408 | 42 | if (preg_match('/^(.*?)(?:\s+AS\s+|\s+)(\w+)$/i', $name, $matches)) { |
|
409 | // relation is defined with an alias, adjust callback to apply alias |
||
410 | 9 | list(, $relation, $alias) = $matches; |
|
411 | 9 | $name = $relation; |
|
412 | 9 | $callback = function ($query) use ($callback, $alias) { |
|
413 | /** @var $query ActiveQuery */ |
||
414 | 9 | $query->alias($alias); |
|
415 | 9 | if ($callback !== null) { |
|
416 | 9 | call_user_func($callback, $query); |
|
417 | } |
||
418 | 9 | }; |
|
419 | } |
||
420 | |||
421 | 42 | if ($callback === null) { |
|
422 | 42 | $relations[] = $name; |
|
423 | } else { |
||
424 | 15 | $relations[$name] = $callback; |
|
425 | } |
||
426 | } |
||
427 | 42 | $this->joinWith[] = [$relations, $eagerLoading, $joinType]; |
|
428 | 42 | return $this; |
|
429 | } |
||
430 | |||
431 | 36 | private function buildJoinWith() |
|
432 | { |
||
433 | 36 | $join = $this->join; |
|
434 | 36 | $this->join = []; |
|
435 | |||
436 | 36 | $model = new $this->modelClass; |
|
437 | 36 | foreach ($this->joinWith as $config) { |
|
438 | 36 | list ($with, $eagerLoading, $joinType) = $config; |
|
439 | 36 | $this->joinWithRelations($model, $with, $joinType); |
|
440 | |||
441 | 36 | if (is_array($eagerLoading)) { |
|
442 | foreach ($with as $name => $callback) { |
||
443 | if (is_int($name)) { |
||
444 | if (!in_array($callback, $eagerLoading, true)) { |
||
445 | unset($with[$name]); |
||
446 | } |
||
447 | } elseif (!in_array($name, $eagerLoading, true)) { |
||
448 | unset($with[$name]); |
||
449 | } |
||
450 | } |
||
451 | 36 | } elseif (!$eagerLoading) { |
|
452 | 12 | $with = []; |
|
453 | } |
||
454 | |||
455 | 36 | $this->with($with); |
|
456 | } |
||
457 | |||
458 | // remove duplicated joins added by joinWithRelations that may be added |
||
459 | // e.g. when joining a relation and a via relation at the same time |
||
460 | 36 | $uniqueJoins = []; |
|
461 | 36 | foreach ($this->join as $j) { |
|
462 | 36 | $uniqueJoins[serialize($j)] = $j; |
|
463 | } |
||
464 | 36 | $this->join = array_values($uniqueJoins); |
|
465 | |||
466 | 36 | if (!empty($join)) { |
|
467 | // append explicit join to joinWith() |
||
468 | // https://github.com/yiisoft/yii2/issues/2880 |
||
469 | $this->join = empty($this->join) ? $join : array_merge($this->join, $join); |
||
470 | } |
||
471 | 36 | } |
|
472 | |||
473 | /** |
||
474 | * Inner joins with the specified relations. |
||
475 | * This is a shortcut method to [[joinWith()]] with the join type set as "INNER JOIN". |
||
476 | * Please refer to [[joinWith()]] for detailed usage of this method. |
||
477 | * @param string|array $with the relations to be joined with. |
||
478 | * @param bool|array $eagerLoading whether to eager loading the relations. |
||
479 | * @return $this the query object itself |
||
480 | * @see joinWith() |
||
481 | */ |
||
482 | 12 | public function innerJoinWith($with, $eagerLoading = true) |
|
486 | |||
487 | /** |
||
488 | * Modifies the current query by adding join fragments based on the given relations. |
||
489 | * @param ActiveRecord $model the primary model |
||
490 | * @param array $with the relations to be joined |
||
491 | * @param string|array $joinType the join type |
||
492 | */ |
||
493 | 36 | private function joinWithRelations($model, $with, $joinType) |
|
494 | { |
||
495 | 36 | $relations = []; |
|
496 | |||
497 | 36 | foreach ($with as $name => $callback) { |
|
498 | 36 | if (is_int($name)) { |
|
499 | 36 | $name = $callback; |
|
500 | 36 | $callback = null; |
|
501 | } |
||
502 | |||
503 | 36 | $primaryModel = $model; |
|
504 | 36 | $parent = $this; |
|
505 | 36 | $prefix = ''; |
|
506 | 36 | while (($pos = strpos($name, '.')) !== false) { |
|
507 | 6 | $childName = substr($name, $pos + 1); |
|
508 | 6 | $name = substr($name, 0, $pos); |
|
509 | 6 | $fullName = $prefix === '' ? $name : "$prefix.$name"; |
|
510 | 6 | if (!isset($relations[$fullName])) { |
|
511 | $relations[$fullName] = $relation = $primaryModel->getRelation($name); |
||
512 | $this->joinWithRelation($parent, $relation, $this->getJoinType($joinType, $fullName)); |
||
513 | } else { |
||
514 | 6 | $relation = $relations[$fullName]; |
|
515 | } |
||
516 | 6 | $primaryModel = new $relation->modelClass; |
|
517 | 6 | $parent = $relation; |
|
518 | 6 | $prefix = $fullName; |
|
519 | 6 | $name = $childName; |
|
520 | } |
||
521 | |||
522 | 36 | $fullName = $prefix === '' ? $name : "$prefix.$name"; |
|
523 | 36 | if (!isset($relations[$fullName])) { |
|
524 | 36 | $relations[$fullName] = $relation = $primaryModel->getRelation($name); |
|
525 | 36 | if ($callback !== null) { |
|
526 | 15 | call_user_func($callback, $relation); |
|
527 | } |
||
528 | 36 | if (!empty($relation->joinWith)) { |
|
529 | 6 | $relation->buildJoinWith(); |
|
530 | } |
||
531 | 36 | $this->joinWithRelation($parent, $relation, $this->getJoinType($joinType, $fullName)); |
|
532 | } |
||
533 | } |
||
534 | 36 | } |
|
535 | |||
536 | /** |
||
537 | * Returns the join type based on the given join type parameter and the relation name. |
||
538 | * @param string|array $joinType the given join type(s) |
||
539 | * @param string $name relation name |
||
540 | * @return string the real join type |
||
541 | */ |
||
542 | 36 | private function getJoinType($joinType, $name) |
|
550 | |||
551 | /** |
||
552 | * Returns the table name and the table alias for [[modelClass]]. |
||
553 | * @return array the table name and the table alias. |
||
554 | * @internal |
||
555 | */ |
||
556 | 51 | private function getTableNameAndAlias() |
|
557 | { |
||
558 | 51 | if (empty($this->from)) { |
|
559 | 45 | $tableName = $this->getPrimaryTableName(); |
|
579 | |||
580 | /** |
||
581 | * Joins a parent query with a child query. |
||
582 | * The current query object will be modified accordingly. |
||
583 | * @param ActiveQuery $parent |
||
584 | * @param ActiveQuery $child |
||
585 | * @param string $joinType |
||
586 | */ |
||
587 | 36 | private function joinWithRelation($parent, $child, $joinType) |
|
654 | |||
655 | /** |
||
656 | * Sets the ON condition for a relational query. |
||
657 | * The condition will be used in the ON part when [[ActiveQuery::joinWith()]] is called. |
||
658 | * Otherwise, the condition will be used in the WHERE part of a query. |
||
659 | * |
||
660 | * Use this method to specify additional conditions when declaring a relation in the [[ActiveRecord]] class: |
||
661 | * |
||
662 | * ```php |
||
663 | * public function getActiveUsers() |
||
664 | * { |
||
665 | * return $this->hasMany(User::className(), ['id' => 'user_id']) |
||
666 | * ->onCondition(['active' => true]); |
||
667 | * } |
||
668 | * ``` |
||
669 | * |
||
670 | * Note that this condition is applied in case of a join as well as when fetching the related records. |
||
671 | * Thus only fields of the related table can be used in the condition. Trying to access fields of the primary |
||
672 | * record will cause an error in a non-join-query. |
||
673 | * |
||
674 | * @param string|array $condition the ON condition. Please refer to [[Query::where()]] on how to specify this parameter. |
||
675 | * @param array $params the parameters (name => value) to be bound to the query. |
||
676 | * @return $this the query object itself |
||
677 | */ |
||
678 | 21 | public function onCondition($condition, $params = []) |
|
684 | |||
685 | /** |
||
686 | * Adds an additional ON condition to the existing one. |
||
687 | * The new condition and the existing one will be joined using the 'AND' operator. |
||
688 | * @param string|array $condition the new ON condition. Please refer to [[where()]] |
||
689 | * on how to specify this parameter. |
||
690 | * @param array $params the parameters (name => value) to be bound to the query. |
||
691 | * @return $this the query object itself |
||
692 | * @see onCondition() |
||
693 | * @see orOnCondition() |
||
694 | */ |
||
695 | 6 | public function andOnCondition($condition, $params = []) |
|
705 | |||
706 | /** |
||
707 | * Adds an additional ON condition to the existing one. |
||
708 | * The new condition and the existing one will be joined using the 'OR' operator. |
||
709 | * @param string|array $condition the new ON condition. Please refer to [[where()]] |
||
710 | * on how to specify this parameter. |
||
711 | * @param array $params the parameters (name => value) to be bound to the query. |
||
712 | * @return $this the query object itself |
||
713 | * @see onCondition() |
||
714 | * @see andOnCondition() |
||
715 | */ |
||
716 | 6 | public function orOnCondition($condition, $params = []) |
|
726 | |||
727 | /** |
||
728 | * Specifies the junction table for a relational query. |
||
729 | * |
||
730 | * Use this method to specify a junction table when declaring a relation in the [[ActiveRecord]] class: |
||
731 | * |
||
732 | * ```php |
||
733 | * public function getItems() |
||
734 | * { |
||
735 | * return $this->hasMany(Item::className(), ['id' => 'item_id']) |
||
736 | * ->viaTable('order_item', ['order_id' => 'id']); |
||
737 | * } |
||
738 | * ``` |
||
739 | * |
||
740 | * @param string $tableName the name of the junction table. |
||
741 | * @param array $link the link between the junction table and the table associated with [[primaryModel]]. |
||
742 | * The keys of the array represent the columns in the junction table, and the values represent the columns |
||
743 | * in the [[primaryModel]] table. |
||
744 | * @param callable $callable a PHP callback for customizing the relation associated with the junction table. |
||
745 | * Its signature should be `function($query)`, where `$query` is the query to be customized. |
||
746 | * @return $this the query object itself |
||
747 | * @see via() |
||
748 | */ |
||
749 | 24 | public function viaTable($tableName, $link, callable $callable = null) |
|
764 | |||
765 | /** |
||
766 | * Define an alias for the table defined in [[modelClass]]. |
||
767 | * |
||
768 | * This method will adjust [[from]] so that an already defined alias will be overwritten. |
||
769 | * If none was defined, [[from]] will be populated with the given alias. |
||
770 | * |
||
771 | * @param string $alias the table alias. |
||
772 | * @return $this the query object itself |
||
773 | * @since 2.0.7 |
||
774 | */ |
||
775 | 18 | public function alias($alias) |
|
792 | |||
793 | /** |
||
794 | * Returns table names used in [[from]] indexed by aliases. |
||
795 | * @return string[] table names indexed by aliases |
||
796 | * @throws \yii\base\InvalidConfigException |
||
797 | * @since 2.0.12 |
||
798 | */ |
||
799 | 69 | public function getTablesUsedInFrom() |
|
834 | |||
835 | /** |
||
836 | * @return string primary table name |
||
837 | * @since 2.0.12 |
||
838 | */ |
||
839 | 331 | protected function getPrimaryTableName() |
|
845 | } |
||
846 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..