Passed
Push — 17667-sqlite-create-index-with... ( 21cdd8...b9d5ca )
by Alexander
84:30 queued 44:33
created

ActiveQuery::buildJoinWith()   C

Complexity

Conditions 13
Paths 72

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 29
nc 72
nop 0
dl 0
loc 51
rs 6.6166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\db;
9
10
use yii\base\InvalidConfigException;
11
12
/**
13
 * ActiveQuery represents a DB query associated with an Active Record class.
14
 *
15
 * An ActiveQuery can be a normal query or be used in a relational context.
16
 *
17
 * ActiveQuery instances are usually created by [[ActiveRecord::find()]] and [[ActiveRecord::findBySql()]].
18
 * Relational queries are created by [[ActiveRecord::hasOne()]] and [[ActiveRecord::hasMany()]].
19
 *
20
 * Normal Query
21
 * ------------
22
 *
23
 * ActiveQuery mainly provides the following methods to retrieve the query results:
24
 *
25
 * - [[one()]]: returns a single record populated with the first row of data.
26
 * - [[all()]]: returns all records based on the query results.
27
 * - [[count()]]: returns the number of records.
28
 * - [[sum()]]: returns the sum over the specified column.
29
 * - [[average()]]: returns the average over the specified column.
30
 * - [[min()]]: returns the min over the specified column.
31
 * - [[max()]]: returns the max over the specified column.
32
 * - [[scalar()]]: returns the value of the first column in the first row of the query result.
33
 * - [[column()]]: returns the value of the first column in the query result.
34
 * - [[exists()]]: returns a value indicating whether the query result has data or not.
35
 *
36
 * Because ActiveQuery extends from [[Query]], one can use query methods, such as [[where()]],
37
 * [[orderBy()]] to customize the query options.
38
 *
39
 * ActiveQuery also provides the following additional query options:
40
 *
41
 * - [[with()]]: list of relations that this query should be performed with.
42
 * - [[joinWith()]]: reuse a relation query definition to add a join to a query.
43
 * - [[indexBy()]]: the name of the column by which the query result should be indexed.
44
 * - [[asArray()]]: whether to return each record as an array.
45
 *
46
 * These options can be configured using methods of the same name. For example:
47
 *
48
 * ```php
49
 * $customers = Customer::find()->with('orders')->asArray()->all();
50
 * ```
51
 *
52
 * Relational query
53
 * ----------------
54
 *
55
 * In relational context ActiveQuery represents a relation between two Active Record classes.
56
 *
57
 * Relational ActiveQuery instances are usually created by calling [[ActiveRecord::hasOne()]] and
58
 * [[ActiveRecord::hasMany()]]. An Active Record class declares a relation by defining
59
 * a getter method which calls one of the above methods and returns the created ActiveQuery object.
60
 *
61
 * A relation is specified by [[link]] which represents the association between columns
62
 * of different tables; and the multiplicity of the relation is indicated by [[multiple]].
63
 *
64
 * If a relation involves a junction table, it may be specified by [[via()]] or [[viaTable()]] method.
65
 * These methods may only be called in a relational context. Same is true for [[inverseOf()]], which
66
 * marks a relation as inverse of another relation and [[onCondition()]] which adds a condition that
67
 * is to be added to relational query join condition.
68
 *
69
 * @author Qiang Xue <[email protected]>
70
 * @author Carsten Brandt <[email protected]>
71
 * @since 2.0
72
 */
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
    public function __construct($modelClass, $config = [])
108
    {
109
        $this->modelClass = $modelClass;
110
        parent::__construct($config);
111
    }
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
    public function init()
120
    {
121
        parent::init();
122
        $this->trigger(self::EVENT_INIT);
123
    }
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
    public function all($db = null)
132
    {
133
        return parent::all($db);
0 ignored issues
show
Unused Code introduced by
The call to yii\db\ActiveRelationTrait::all() has too many arguments starting with $db. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

133
        return parent::/** @scrutinizer ignore-call */ all($db);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    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
        if (!empty($this->joinWith)) {
146
            $this->buildJoinWith();
147
            $this->joinWith = null;    // clean it up to avoid issue https://github.com/yiisoft/yii2/issues/2687
148
        }
149
150
        if (empty($this->from)) {
151
            $this->from = [$this->getPrimaryTableName()];
152
        }
153
154
        if (empty($this->select) && !empty($this->join)) {
155
            list(, $alias) = $this->getTableNameAndAlias();
156
            $this->select = ["$alias.*"];
157
        }
158
159
        if ($this->primaryModel === null) {
160
            // eager loading
161
            $query = Query::create($this);
162
        } else {
163
            // lazy loading of a relation
164
            $where = $this->where;
165
166
            if ($this->via instanceof self) {
167
                // via junction table
168
                $viaModels = $this->via->findJunctionRows([$this->primaryModel]);
169
                $this->filterByModels($viaModels);
170
            } elseif (is_array($this->via)) {
171
                // via relation
172
                /* @var $viaQuery ActiveQuery */
173
                list($viaName, $viaQuery, $viaCallableUsed) = $this->via;
174
                if ($viaQuery->multiple) {
175
                    if ($viaCallableUsed) {
176
                        $viaModels = $viaQuery->all();
177
                    } elseif ($this->primaryModel->isRelationPopulated($viaName)) {
178
                        $viaModels = $this->primaryModel->$viaName;
179
                    } else {
180
                        $viaModels = $viaQuery->all();
181
                        $this->primaryModel->populateRelation($viaName, $viaModels);
182
                    }
183
                } else {
184
                    if ($viaCallableUsed) {
185
                        $model = $viaQuery->one();
186
                    } elseif ($this->primaryModel->isRelationPopulated($viaName)) {
187
                        $model = $this->primaryModel->$viaName;
188
                    } else {
189
                        $model = $viaQuery->one();
190
                        $this->primaryModel->populateRelation($viaName, $model);
191
                    }
192
                    $viaModels = $model === null ? [] : [$model];
193
                }
194
                $this->filterByModels($viaModels);
195
            } else {
196
                $this->filterByModels([$this->primaryModel]);
197
            }
198
199
            $query = Query::create($this);
200
            $this->where = $where;
201
        }
202
203
        if (!empty($this->on)) {
204
            $query->andWhere($this->on);
205
        }
206
207
        return $query;
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    public function populate($rows)
214
    {
215
        if (empty($rows)) {
216
            return [];
217
        }
218
219
        $models = $this->createModels($rows);
220
        if (!empty($this->join) && $this->indexBy === null) {
221
            $models = $this->removeDuplicatedModels($models);
222
        }
223
        if (!empty($this->with)) {
224
            $this->findWith($this->with, $models);
225
        }
226
227
        if ($this->inverseOf !== null) {
228
            $this->addInverseRelations($models);
229
        }
230
231
        if (!$this->asArray) {
232
            foreach ($models as $model) {
233
                $model->afterFind();
234
            }
235
        }
236
237
        return parent::populate($models);
238
    }
239
240
    /**
241
     * Removes duplicated models by checking their primary key values.
242
     * This method is mainly called when a join query is performed, which may cause duplicated rows being returned.
243
     * @param array $models the models to be checked
244
     * @throws InvalidConfigException if model primary key is empty
245
     * @return array the distinctive models
246
     */
247
    private function removeDuplicatedModels($models)
248
    {
249
        $hash = [];
250
        /* @var $class ActiveRecord */
251
        $class = $this->modelClass;
252
        $pks = $class::primaryKey();
253
254
        if (count($pks) > 1) {
255
            // composite primary key
256
            foreach ($models as $i => $model) {
257
                $key = [];
258
                foreach ($pks as $pk) {
259
                    if (!isset($model[$pk])) {
260
                        // do not continue if the primary key is not part of the result set
261
                        break 2;
262
                    }
263
                    $key[] = $model[$pk];
264
                }
265
                $key = serialize($key);
266
                if (isset($hash[$key])) {
267
                    unset($models[$i]);
268
                } else {
269
                    $hash[$key] = true;
270
                }
271
            }
272
        } elseif (empty($pks)) {
273
            throw new InvalidConfigException("Primary key of '{$class}' can not be empty.");
274
        } else {
275
            // single column primary key
276
            $pk = reset($pks);
277
            foreach ($models as $i => $model) {
278
                if (!isset($model[$pk])) {
279
                    // do not continue if the primary key is not part of the result set
280
                    break;
281
                }
282
                $key = $model[$pk];
283
                if (isset($hash[$key])) {
284
                    unset($models[$i]);
285
                } elseif ($key !== null) {
286
                    $hash[$key] = true;
287
                }
288
            }
289
        }
290
291
        return array_values($models);
292
    }
293
294
    /**
295
     * Executes query and returns a single row of result.
296
     * @param Connection|null $db the DB connection used to create the DB command.
297
     * If `null`, the DB connection returned by [[modelClass]] will be used.
298
     * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
299
     * the query result may be either an array or an ActiveRecord object. `null` will be returned
300
     * if the query results in nothing.
301
     */
302
    public function one($db = null)
303
    {
304
        $row = parent::one($db);
0 ignored issues
show
Unused Code introduced by
The call to yii\db\ActiveRelationTrait::one() has too many arguments starting with $db. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

304
        /** @scrutinizer ignore-call */ 
305
        $row = parent::one($db);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
305
        if ($row !== false) {
0 ignored issues
show
introduced by
The condition $row !== false is always true.
Loading history...
306
            $models = $this->populate([$row]);
307
            return reset($models) ?: null;
308
        }
309
310
        return null;
311
    }
312
313
    /**
314
     * Creates a DB command that can be used to execute this query.
315
     * @param Connection|null $db the DB connection used to create the DB command.
316
     * If `null`, the DB connection returned by [[modelClass]] will be used.
317
     * @return Command the created DB command instance.
318
     */
319
    public function createCommand($db = null)
320
    {
321
        /* @var $modelClass ActiveRecord */
322
        $modelClass = $this->modelClass;
323
        if ($db === null) {
324
            $db = $modelClass::getDb();
325
        }
326
327
        if ($this->sql === null) {
328
            list($sql, $params) = $db->getQueryBuilder()->build($this);
329
        } else {
330
            $sql = $this->sql;
331
            $params = $this->params;
332
        }
333
334
        $command = $db->createCommand($sql, $params);
335
        $this->setCommandCache($command);
336
337
        return $command;
338
    }
339
340
    /**
341
     * {@inheritdoc}
342
     */
343
    protected function queryScalar($selectExpression, $db)
344
    {
345
        /* @var $modelClass ActiveRecord */
346
        $modelClass = $this->modelClass;
347
        if ($db === null) {
348
            $db = $modelClass::getDb();
349
        }
350
351
        if ($this->sql === null) {
352
            return parent::queryScalar($selectExpression, $db);
353
        }
354
355
        $command = (new Query())->select([$selectExpression])
356
            ->from(['c' => "({$this->sql})"])
357
            ->params($this->params)
358
            ->createCommand($db);
359
        $this->setCommandCache($command);
360
361
        return $command->queryScalar();
362
    }
363
364
    /**
365
     * Joins with the specified relations.
366
     *
367
     * This method allows you to reuse existing relation definitions to perform JOIN queries.
368
     * Based on the definition of the specified relation(s), the method will append one or multiple
369
     * JOIN statements to the current query.
370
     *
371
     * If the `$eagerLoading` parameter is true, the method will also perform eager loading for the specified relations,
372
     * which is equivalent to calling [[with()]] using the specified relations.
373
     *
374
     * Note that because a JOIN query will be performed, you are responsible to disambiguate column names.
375
     *
376
     * This method differs from [[with()]] in that it will build up and execute a JOIN SQL statement
377
     * for the primary table. And when `$eagerLoading` is true, it will call [[with()]] in addition with the specified relations.
378
     *
379
     * @param string|array $with the relations to be joined. This can either be a string, representing a relation name or
380
     * an array with the following semantics:
381
     *
382
     * - Each array element represents a single relation.
383
     * - You may specify the relation name as the array key and provide an anonymous functions that
384
     *   can be used to modify the relation queries on-the-fly as the array value.
385
     * - If a relation query does not need modification, you may use the relation name as the array value.
386
     *
387
     * The relation name may optionally contain an alias for the relation table (e.g. `books b`).
388
     *
389
     * Sub-relations can also be specified, see [[with()]] for the syntax.
390
     *
391
     * In the following you find some examples:
392
     *
393
     * ```php
394
     * // find all orders that contain books, and eager loading "books"
395
     * Order::find()->joinWith('books', true, 'INNER JOIN')->all();
396
     * // find all orders, eager loading "books", and sort the orders and books by the book names.
397
     * Order::find()->joinWith([
398
     *     'books' => function (\yii\db\ActiveQuery $query) {
399
     *         $query->orderBy('item.name');
400
     *     }
401
     * ])->all();
402
     * // find all orders that contain books of the category 'Science fiction', using the alias "b" for the books table
403
     * Order::find()->joinWith(['books b'], true, 'INNER JOIN')->where(['b.category' => 'Science fiction'])->all();
404
     * ```
405
     *
406
     * The alias syntax is available since version 2.0.7.
407
     *
408
     * @param bool|array $eagerLoading whether to eager load the relations
409
     * specified in `$with`.  When this is a boolean, it applies to all
410
     * relations specified in `$with`. Use an array to explicitly list which
411
     * relations in `$with` need to be eagerly loaded.  Note, that this does
412
     * not mean, that the relations are populated from the query result. An
413
     * extra query will still be performed to bring in the related data.
414
     * Defaults to `true`.
415
     * @param string|array $joinType the join type of the relations specified in `$with`.
416
     * When this is a string, it applies to all relations specified in `$with`. Use an array
417
     * in the format of `relationName => joinType` to specify different join types for different relations.
418
     * @return $this the query object itself
419
     */
420
    public function joinWith($with, $eagerLoading = true, $joinType = 'LEFT JOIN')
421
    {
422
        $relations = [];
423
        foreach ((array) $with as $name => $callback) {
424
            if (is_int($name)) {
425
                $name = $callback;
426
                $callback = null;
427
            }
428
429
            if (preg_match('/^(.*?)(?:\s+AS\s+|\s+)(\w+)$/i', $name, $matches)) {
430
                // relation is defined with an alias, adjust callback to apply alias
431
                list(, $relation, $alias) = $matches;
432
                $name = $relation;
433
                $callback = function ($query) use ($callback, $alias) {
434
                    /* @var $query ActiveQuery */
435
                    $query->alias($alias);
436
                    if ($callback !== null) {
437
                        call_user_func($callback, $query);
438
                    }
439
                };
440
            }
441
442
            if ($callback === null) {
443
                $relations[] = $name;
444
            } else {
445
                $relations[$name] = $callback;
446
            }
447
        }
448
        $this->joinWith[] = [$relations, $eagerLoading, $joinType];
449
        return $this;
450
    }
451
452
    private function buildJoinWith()
453
    {
454
        $join = $this->join;
455
        $this->join = [];
456
457
        /* @var $modelClass ActiveRecordInterface */
458
        $modelClass = $this->modelClass;
459
        $model = $modelClass::instance();
460
        foreach ($this->joinWith as $config) {
461
            list($with, $eagerLoading, $joinType) = $config;
462
            $this->joinWithRelations($model, $with, $joinType);
463
464
            if (is_array($eagerLoading)) {
465
                foreach ($with as $name => $callback) {
466
                    if (is_int($name)) {
467
                        if (!in_array($callback, $eagerLoading, true)) {
468
                            unset($with[$name]);
469
                        }
470
                    } elseif (!in_array($name, $eagerLoading, true)) {
471
                        unset($with[$name]);
472
                    }
473
                }
474
            } elseif (!$eagerLoading) {
475
                $with = [];
476
            }
477
478
            $this->with($with);
479
        }
480
481
        // remove duplicated joins added by joinWithRelations that may be added
482
        // e.g. when joining a relation and a via relation at the same time
483
        $uniqueJoins = [];
484
        foreach ($this->join as $j) {
485
            $uniqueJoins[serialize($j)] = $j;
486
        }
487
        $this->join = array_values($uniqueJoins);
488
489
        // https://github.com/yiisoft/yii2/issues/16092
490
        $uniqueJoinsByTableName = [];
491
        foreach ($this->join as $config) {
492
            $tableName = serialize($config[1]);
493
            if (!array_key_exists($tableName, $uniqueJoinsByTableName)) {
494
                $uniqueJoinsByTableName[$tableName] = $config;
495
            }
496
        }
497
        $this->join = array_values($uniqueJoinsByTableName);
498
499
        if (!empty($join)) {
500
            // append explicit join to joinWith()
501
            // https://github.com/yiisoft/yii2/issues/2880
502
            $this->join = empty($this->join) ? $join : array_merge($this->join, $join);
503
        }
504
    }
505
506
    /**
507
     * Inner joins with the specified relations.
508
     * This is a shortcut method to [[joinWith()]] with the join type set as "INNER JOIN".
509
     * Please refer to [[joinWith()]] for detailed usage of this method.
510
     * @param string|array $with the relations to be joined with.
511
     * @param bool|array $eagerLoading whether to eager load the relations.
512
     * Note, that this does not mean, that the relations are populated from the
513
     * query result. An extra query will still be performed to bring in the
514
     * related data.
515
     * @return $this the query object itself
516
     * @see joinWith()
517
     */
518
    public function innerJoinWith($with, $eagerLoading = true)
519
    {
520
        return $this->joinWith($with, $eagerLoading, 'INNER JOIN');
521
    }
522
523
    /**
524
     * Modifies the current query by adding join fragments based on the given relations.
525
     * @param ActiveRecord $model the primary model
526
     * @param array $with the relations to be joined
527
     * @param string|array $joinType the join type
528
     */
529
    private function joinWithRelations($model, $with, $joinType)
530
    {
531
        $relations = [];
532
533
        foreach ($with as $name => $callback) {
534
            if (is_int($name)) {
535
                $name = $callback;
536
                $callback = null;
537
            }
538
539
            $primaryModel = $model;
540
            $parent = $this;
541
            $prefix = '';
542
            while (($pos = strpos($name, '.')) !== false) {
543
                $childName = substr($name, $pos + 1);
544
                $name = substr($name, 0, $pos);
545
                $fullName = $prefix === '' ? $name : "$prefix.$name";
546
                if (!isset($relations[$fullName])) {
547
                    $relations[$fullName] = $relation = $primaryModel->getRelation($name);
548
                    $this->joinWithRelation($parent, $relation, $this->getJoinType($joinType, $fullName));
549
                } else {
550
                    $relation = $relations[$fullName];
551
                }
552
                /* @var $relationModelClass ActiveRecordInterface */
553
                $relationModelClass = $relation->modelClass;
0 ignored issues
show
Bug introduced by
Accessing modelClass on the interface yii\db\ActiveQueryInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
554
                $primaryModel = $relationModelClass::instance();
555
                $parent = $relation;
556
                $prefix = $fullName;
557
                $name = $childName;
558
            }
559
560
            $fullName = $prefix === '' ? $name : "$prefix.$name";
561
            if (!isset($relations[$fullName])) {
562
                $relations[$fullName] = $relation = $primaryModel->getRelation($name);
563
                if ($callback !== null) {
564
                    call_user_func($callback, $relation);
565
                }
566
                if (!empty($relation->joinWith)) {
0 ignored issues
show
Bug introduced by
Accessing joinWith on the interface yii\db\ActiveQueryInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
567
                    $relation->buildJoinWith();
0 ignored issues
show
Bug introduced by
The method buildJoinWith() does not exist on yii\db\ActiveQueryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to yii\db\ActiveQueryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

567
                    $relation->/** @scrutinizer ignore-call */ 
568
                               buildJoinWith();
Loading history...
568
                }
569
                $this->joinWithRelation($parent, $relation, $this->getJoinType($joinType, $fullName));
570
            }
571
        }
572
    }
573
574
    /**
575
     * Returns the join type based on the given join type parameter and the relation name.
576
     * @param string|array $joinType the given join type(s)
577
     * @param string $name relation name
578
     * @return string the real join type
579
     */
580
    private function getJoinType($joinType, $name)
581
    {
582
        if (is_array($joinType) && isset($joinType[$name])) {
583
            return $joinType[$name];
584
        }
585
586
        return is_string($joinType) ? $joinType : 'INNER JOIN';
587
    }
588
589
    /**
590
     * Returns the table name and the table alias for [[modelClass]].
591
     * @return array the table name and the table alias.
592
     * @since 2.0.16
593
     */
594
    protected function getTableNameAndAlias()
595
    {
596
        if (empty($this->from)) {
597
            $tableName = $this->getPrimaryTableName();
598
        } else {
599
            $tableName = '';
600
            // if the first entry in "from" is an alias-tablename-pair return it directly
601
            foreach ($this->from as $alias => $tableName) {
602
                if (is_string($alias)) {
603
                    return [$tableName, $alias];
604
                }
605
                break;
606
            }
607
        }
608
609
        if (preg_match('/^(.*?)\s+({{\w+}}|\w+)$/', $tableName, $matches)) {
610
            $alias = $matches[2];
611
        } else {
612
            $alias = $tableName;
613
        }
614
615
        return [$tableName, $alias];
616
    }
617
618
    /**
619
     * Joins a parent query with a child query.
620
     * The current query object will be modified accordingly.
621
     * @param ActiveQuery $parent
622
     * @param ActiveQuery $child
623
     * @param string $joinType
624
     */
625
    private function joinWithRelation($parent, $child, $joinType)
626
    {
627
        $via = $child->via;
628
        $child->via = null;
629
        if ($via instanceof self) {
630
            // via table
631
            $this->joinWithRelation($parent, $via, $joinType);
632
            $this->joinWithRelation($via, $child, $joinType);
633
            return;
634
        } elseif (is_array($via)) {
635
            // via relation
636
            $this->joinWithRelation($parent, $via[1], $joinType);
637
            $this->joinWithRelation($via[1], $child, $joinType);
638
            return;
639
        }
640
641
        list($parentTable, $parentAlias) = $parent->getTableNameAndAlias();
642
        list($childTable, $childAlias) = $child->getTableNameAndAlias();
643
644
        if (!empty($child->link)) {
645
            if (strpos($parentAlias, '{{') === false) {
646
                $parentAlias = '{{' . $parentAlias . '}}';
647
            }
648
            if (strpos($childAlias, '{{') === false) {
649
                $childAlias = '{{' . $childAlias . '}}';
650
            }
651
652
            $on = [];
653
            foreach ($child->link as $childColumn => $parentColumn) {
654
                $on[] = "$parentAlias.[[$parentColumn]] = $childAlias.[[$childColumn]]";
655
            }
656
            $on = implode(' AND ', $on);
657
            if (!empty($child->on)) {
658
                $on = ['and', $on, $child->on];
659
            }
660
        } else {
661
            $on = $child->on;
662
        }
663
        $this->join($joinType, empty($child->from) ? $childTable : $child->from, $on);
664
665
        if (!empty($child->where)) {
666
            $this->andWhere($child->where);
667
        }
668
        if (!empty($child->having)) {
669
            $this->andHaving($child->having);
670
        }
671
        if (!empty($child->orderBy)) {
672
            $this->addOrderBy($child->orderBy);
673
        }
674
        if (!empty($child->groupBy)) {
675
            $this->addGroupBy($child->groupBy);
676
        }
677
        if (!empty($child->params)) {
678
            $this->addParams($child->params);
679
        }
680
        if (!empty($child->join)) {
681
            foreach ($child->join as $join) {
682
                $this->join[] = $join;
683
            }
684
        }
685
        if (!empty($child->union)) {
686
            foreach ($child->union as $union) {
687
                $this->union[] = $union;
688
            }
689
        }
690
    }
691
692
    /**
693
     * Sets the ON condition for a relational query.
694
     * The condition will be used in the ON part when [[ActiveQuery::joinWith()]] is called.
695
     * Otherwise, the condition will be used in the WHERE part of a query.
696
     *
697
     * Use this method to specify additional conditions when declaring a relation in the [[ActiveRecord]] class:
698
     *
699
     * ```php
700
     * public function getActiveUsers()
701
     * {
702
     *     return $this->hasMany(User::className(), ['id' => 'user_id'])
703
     *                 ->onCondition(['active' => true]);
704
     * }
705
     * ```
706
     *
707
     * Note that this condition is applied in case of a join as well as when fetching the related records.
708
     * Thus only fields of the related table can be used in the condition. Trying to access fields of the primary
709
     * record will cause an error in a non-join-query.
710
     *
711
     * @param string|array $condition the ON condition. Please refer to [[Query::where()]] on how to specify this parameter.
712
     * @param array $params the parameters (name => value) to be bound to the query.
713
     * @return $this the query object itself
714
     */
715
    public function onCondition($condition, $params = [])
716
    {
717
        $this->on = $condition;
718
        $this->addParams($params);
719
        return $this;
720
    }
721
722
    /**
723
     * Adds an additional ON condition to the existing one.
724
     * The new condition and the existing one will be joined using the 'AND' operator.
725
     * @param string|array $condition the new ON condition. Please refer to [[where()]]
726
     * on how to specify this parameter.
727
     * @param array $params the parameters (name => value) to be bound to the query.
728
     * @return $this the query object itself
729
     * @see onCondition()
730
     * @see orOnCondition()
731
     */
732
    public function andOnCondition($condition, $params = [])
733
    {
734
        if ($this->on === null) {
735
            $this->on = $condition;
736
        } else {
737
            $this->on = ['and', $this->on, $condition];
738
        }
739
        $this->addParams($params);
740
        return $this;
741
    }
742
743
    /**
744
     * Adds an additional ON condition to the existing one.
745
     * The new condition and the existing one will be joined using the 'OR' operator.
746
     * @param string|array $condition the new ON condition. Please refer to [[where()]]
747
     * on how to specify this parameter.
748
     * @param array $params the parameters (name => value) to be bound to the query.
749
     * @return $this the query object itself
750
     * @see onCondition()
751
     * @see andOnCondition()
752
     */
753
    public function orOnCondition($condition, $params = [])
754
    {
755
        if ($this->on === null) {
756
            $this->on = $condition;
757
        } else {
758
            $this->on = ['or', $this->on, $condition];
759
        }
760
        $this->addParams($params);
761
        return $this;
762
    }
763
764
    /**
765
     * Specifies the junction table for a relational query.
766
     *
767
     * Use this method to specify a junction table when declaring a relation in the [[ActiveRecord]] class:
768
     *
769
     * ```php
770
     * public function getItems()
771
     * {
772
     *     return $this->hasMany(Item::className(), ['id' => 'item_id'])
773
     *                 ->viaTable('order_item', ['order_id' => 'id']);
774
     * }
775
     * ```
776
     *
777
     * @param string $tableName the name of the junction table.
778
     * @param array $link the link between the junction table and the table associated with [[primaryModel]].
779
     * The keys of the array represent the columns in the junction table, and the values represent the columns
780
     * in the [[primaryModel]] table.
781
     * @param callable $callable a PHP callback for customizing the relation associated with the junction table.
782
     * Its signature should be `function($query)`, where `$query` is the query to be customized.
783
     * @return $this the query object itself
784
     * @throws InvalidConfigException when query is not initialized properly
785
     * @see via()
786
     */
787
    public function viaTable($tableName, $link, callable $callable = null)
788
    {
789
        $modelClass = $this->primaryModel ? get_class($this->primaryModel) : $this->modelClass;
790
        $relation = new self($modelClass, [
791
            'from' => [$tableName],
792
            'link' => $link,
793
            'multiple' => true,
794
            'asArray' => true,
795
        ]);
796
        $this->via = $relation;
797
        if ($callable !== null) {
798
            call_user_func($callable, $relation);
799
        }
800
801
        return $this;
802
    }
803
804
    /**
805
     * Define an alias for the table defined in [[modelClass]].
806
     *
807
     * This method will adjust [[from]] so that an already defined alias will be overwritten.
808
     * If none was defined, [[from]] will be populated with the given alias.
809
     *
810
     * @param string $alias the table alias.
811
     * @return $this the query object itself
812
     * @since 2.0.7
813
     */
814
    public function alias($alias)
815
    {
816
        if (empty($this->from) || count($this->from) < 2) {
817
            list($tableName) = $this->getTableNameAndAlias();
818
            $this->from = [$alias => $tableName];
819
        } else {
820
            $tableName = $this->getPrimaryTableName();
821
822
            foreach ($this->from as $key => $table) {
823
                if ($table === $tableName) {
824
                    unset($this->from[$key]);
825
                    $this->from[$alias] = $tableName;
826
                }
827
            }
828
        }
829
830
        return $this;
831
    }
832
833
    /**
834
     * {@inheritdoc}
835
     * @since 2.0.12
836
     */
837
    public function getTablesUsedInFrom()
838
    {
839
        if (empty($this->from)) {
840
            return $this->cleanUpTableNames([$this->getPrimaryTableName()]);
841
        }
842
843
        return parent::getTablesUsedInFrom();
844
    }
845
846
    /**
847
     * @return string primary table name
848
     * @since 2.0.12
849
     */
850
    protected function getPrimaryTableName()
851
    {
852
        /* @var $modelClass ActiveRecord */
853
        $modelClass = $this->modelClass;
854
        return $modelClass::tableName();
855
    }
856
}
857