Completed
Push — master ( dfcf03...025dd0 )
by Dmitry
13:31
created

ActiveQuery::alias()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.3554
c 0
b 0
f 0
ccs 10
cts 10
cp 1
cc 5
nc 2
nop 1
crap 5
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 519
    public function __construct($modelClass, $config = [])
108
    {
109 519
        $this->modelClass = $modelClass;
110 519
        parent::__construct($config);
111 519
    }
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 519
    public function init()
120
    {
121 519
        parent::init();
122 519
        $this->trigger(self::EVENT_INIT);
123 519
    }
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 204
    public function all($db = null)
132
    {
133 204
        return parent::all($db);
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 406
    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 406
        if (!empty($this->joinWith)) {
146 51
            $this->buildJoinWith();
147 51
            $this->joinWith = null;    // clean it up to avoid issue https://github.com/yiisoft/yii2/issues/2687
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $joinWith.

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..

Loading history...
148
        }
149
150 406
        if (empty($this->from)) {
151 403
            $this->from = [$this->getPrimaryTableName()];
152
        }
153
154 406
        if (empty($this->select) && !empty($this->join)) {
155 42
            list(, $alias) = $this->getTableNameAndAlias();
156 42
            $this->select = ["$alias.*"];
157
        }
158
159 406
        if ($this->primaryModel === null) {
160
            // eager loading
161 399
            $query = Query::create($this);
162
        } else {
163
            // lazy loading of a relation
164 109
            $where = $this->where;
165
166 109
            if ($this->via instanceof self) {
167
                // via junction table
168 15
                $viaModels = $this->via->findJunctionRows([$this->primaryModel]);
169 15
                $this->filterByModels($viaModels);
170 100
            } elseif (is_array($this->via)) {
171
                // via relation
172
                /* @var $viaQuery ActiveQuery */
173 30
                list($viaName, $viaQuery) = $this->via;
174 30
                if ($viaQuery->multiple) {
175 30
                    if ($this->primaryModel->isRelationPopulated($viaName)) {
176 9
                        $viaModels = $this->primaryModel->$viaName;
177
                    } else {
178 30
                        $viaModels = $viaQuery->all();
179 30
                        $this->primaryModel->populateRelation($viaName, $viaModels);
180
                    }
181
                } else {
182
                    if ($this->primaryModel->isRelationPopulated($viaName)) {
183
                        $model = $this->primaryModel->$viaName;
184
                    } else {
185
                        $model = $viaQuery->one();
186
                        $this->primaryModel->populateRelation($viaName, $model);
187
                    }
188
                    $viaModels = $model === null ? [] : [$model];
189
                }
190 30
                $this->filterByModels($viaModels);
191
            } else {
192 100
                $this->filterByModels([$this->primaryModel]);
193
            }
194
195 109
            $query = Query::create($this);
196 109
            $this->where = $where;
197
        }
198
199 406
        if (!empty($this->on)) {
200 18
            $query->andWhere($this->on);
201
        }
202
203 406
        return $query;
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209 350
    public function populate($rows)
210
    {
211 350
        if (empty($rows)) {
212 58
            return [];
213
        }
214
215 338
        $models = $this->createModels($rows);
216 338
        if (!empty($this->join) && $this->indexBy === null) {
217 36
            $models = $this->removeDuplicatedModels($models);
218
        }
219 338
        if (!empty($this->with)) {
220 93
            $this->findWith($this->with, $models);
221
        }
222
223 338
        if ($this->inverseOf !== null) {
224 12
            $this->addInverseRelations($models);
225
        }
226
227 338
        if (!$this->asArray) {
228 328
            foreach ($models as $model) {
229 328
                $model->afterFind();
230
            }
231
        }
232
233 338
        return parent::populate($models);
234
    }
235
236
    /**
237
     * Removes duplicated models by checking their primary key values.
238
     * This method is mainly called when a join query is performed, which may cause duplicated rows being returned.
239
     * @param array $models the models to be checked
240
     * @throws InvalidConfigException if model primary key is empty
241
     * @return array the distinctive models
242
     */
243 36
    private function removeDuplicatedModels($models)
244
    {
245 36
        $hash = [];
246
        /* @var $class ActiveRecord */
247 36
        $class = $this->modelClass;
248 36
        $pks = $class::primaryKey();
249
250 36
        if (count($pks) > 1) {
251
            // composite primary key
252 6
            foreach ($models as $i => $model) {
253 6
                $key = [];
254 6
                foreach ($pks as $pk) {
255 6
                    if (!isset($model[$pk])) {
256
                        // do not continue if the primary key is not part of the result set
257 3
                        break 2;
258
                    }
259 6
                    $key[] = $model[$pk];
260
                }
261 3
                $key = serialize($key);
262 3
                if (isset($hash[$key])) {
263
                    unset($models[$i]);
264
                } else {
265 6
                    $hash[$key] = true;
266
                }
267
            }
268
        } elseif (empty($pks)) {
269
            throw new InvalidConfigException("Primary key of '{$class}' can not be empty.");
270
        } else {
271
            // single column primary key
272 33
            $pk = reset($pks);
273 33
            foreach ($models as $i => $model) {
274 33
                if (!isset($model[$pk])) {
275
                    // do not continue if the primary key is not part of the result set
276 3
                    break;
277
                }
278 30
                $key = $model[$pk];
279 30
                if (isset($hash[$key])) {
280 12
                    unset($models[$i]);
281 30
                } elseif ($key !== null) {
282 30
                    $hash[$key] = true;
283
                }
284
            }
285
        }
286
287 36
        return array_values($models);
288
    }
289
290
    /**
291
     * Executes query and returns a single row of result.
292
     * @param Connection|null $db the DB connection used to create the DB command.
293
     * If `null`, the DB connection returned by [[modelClass]] will be used.
294
     * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
295
     * the query result may be either an array or an ActiveRecord object. `null` will be returned
296
     * if the query results in nothing.
297
     */
298 285
    public function one($db = null)
299
    {
300 285
        $row = parent::one($db);
301 285
        if ($row !== false) {
302 282
            $models = $this->populate([$row]);
303 282
            return reset($models) ?: null;
304
        }
305
306 27
        return null;
307
    }
308
309
    /**
310
     * Creates a DB command that can be used to execute this query.
311
     * @param Connection|null $db the DB connection used to create the DB command.
312
     * If `null`, the DB connection returned by [[modelClass]] will be used.
313
     * @return Command the created DB command instance.
314
     */
315 406
    public function createCommand($db = null)
316
    {
317
        /* @var $modelClass ActiveRecord */
318 406
        $modelClass = $this->modelClass;
319 406
        if ($db === null) {
320 395
            $db = $modelClass::getDb();
321
        }
322
323 406
        if ($this->sql === null) {
324 403
            list($sql, $params) = $db->getQueryBuilder()->build($this);
325
        } else {
326 3
            $sql = $this->sql;
327 3
            $params = $this->params;
328
        }
329
330 406
        $command = $db->createCommand($sql, $params);
331 406
        $this->setCommandCache($command);
332
333 406
        return $command;
334
    }
335
336
    /**
337
     * {@inheritdoc}
338
     */
339 64
    protected function queryScalar($selectExpression, $db)
340
    {
341
        /* @var $modelClass ActiveRecord */
342 64
        $modelClass = $this->modelClass;
343 64
        if ($db === null) {
344 63
            $db = $modelClass::getDb();
345
        }
346
347 64
        if ($this->sql === null) {
348 61
            return parent::queryScalar($selectExpression, $db);
349
        }
350
351 3
        $command = (new Query())->select([$selectExpression])
352 3
            ->from(['c' => "({$this->sql})"])
353 3
            ->params($this->params)
354 3
            ->createCommand($db);
355 3
        $this->setCommandCache($command);
356
357 3
        return $command->queryScalar();
358
    }
359
360
    /**
361
     * Joins with the specified relations.
362
     *
363
     * This method allows you to reuse existing relation definitions to perform JOIN queries.
364
     * Based on the definition of the specified relation(s), the method will append one or multiple
365
     * JOIN statements to the current query.
366
     *
367
     * If the `$eagerLoading` parameter is true, the method will also perform eager loading for the specified relations,
368
     * which is equivalent to calling [[with()]] using the specified relations.
369
     *
370
     * Note that because a JOIN query will be performed, you are responsible to disambiguate column names.
371
     *
372
     * This method differs from [[with()]] in that it will build up and execute a JOIN SQL statement
373
     * for the primary table. And when `$eagerLoading` is true, it will call [[with()]] in addition with the specified relations.
374
     *
375
     * @param string|array $with the relations to be joined. This can either be a string, representing a relation name or
376
     * an array with the following semantics:
377
     *
378
     * - Each array element represents a single relation.
379
     * - You may specify the relation name as the array key and provide an anonymous functions that
380
     *   can be used to modify the relation queries on-the-fly as the array value.
381
     * - If a relation query does not need modification, you may use the relation name as the array value.
382
     *
383
     * The relation name may optionally contain an alias for the relation table (e.g. `books b`).
384
     *
385
     * Sub-relations can also be specified, see [[with()]] for the syntax.
386
     *
387
     * In the following you find some examples:
388
     *
389
     * ```php
390
     * // find all orders that contain books, and eager loading "books"
391
     * Order::find()->joinWith('books', true, 'INNER JOIN')->all();
392
     * // find all orders, eager loading "books", and sort the orders and books by the book names.
393
     * Order::find()->joinWith([
394
     *     'books' => function (\yii\db\ActiveQuery $query) {
395
     *         $query->orderBy('item.name');
396
     *     }
397
     * ])->all();
398
     * // find all orders that contain books of the category 'Science fiction', using the alias "b" for the books table
399
     * Order::find()->joinWith(['books b'], true, 'INNER JOIN')->where(['b.category' => 'Science fiction'])->all();
400
     * ```
401
     *
402
     * The alias syntax is available since version 2.0.7.
403
     *
404
     * @param bool|array $eagerLoading whether to eager load the relations
405
     * specified in `$with`.  When this is a boolean, it applies to all
406
     * relations specified in `$with`. Use an array to explicitly list which
407
     * relations in `$with` need to be eagerly loaded.  Note, that this does
408
     * not mean, that the relations are populated from the query result. An
409
     * extra query will still be performed to bring in the related data.
410
     * Defaults to `true`.
411
     * @param string|array $joinType the join type of the relations specified in `$with`.
412
     * When this is a string, it applies to all relations specified in `$with`. Use an array
413
     * in the format of `relationName => joinType` to specify different join types for different relations.
414
     * @return $this the query object itself
415
     */
416 57
    public function joinWith($with, $eagerLoading = true, $joinType = 'LEFT JOIN')
417
    {
418 57
        $relations = [];
419 57
        foreach ((array) $with as $name => $callback) {
420 57
            if (is_int($name)) {
421 57
                $name = $callback;
422 57
                $callback = null;
423
            }
424
425 57
            if (preg_match('/^(.*?)(?:\s+AS\s+|\s+)(\w+)$/i', $name, $matches)) {
426
                // relation is defined with an alias, adjust callback to apply alias
427 12
                list(, $relation, $alias) = $matches;
428 12
                $name = $relation;
429 12
                $callback = function ($query) use ($callback, $alias) {
430
                    /* @var $query ActiveQuery */
431 12
                    $query->alias($alias);
432 12
                    if ($callback !== null) {
433 9
                        call_user_func($callback, $query);
434
                    }
435 12
                };
436
            }
437
438 57
            if ($callback === null) {
439 54
                $relations[] = $name;
440
            } else {
441 57
                $relations[$name] = $callback;
442
            }
443
        }
444 57
        $this->joinWith[] = [$relations, $eagerLoading, $joinType];
445 57
        return $this;
446
    }
447
448 51
    private function buildJoinWith()
449
    {
450 51
        $join = $this->join;
451 51
        $this->join = [];
452
453
        /* @var $modelClass ActiveRecordInterface */
454 51
        $modelClass = $this->modelClass;
455 51
        $model = $modelClass::instance();
456 51
        foreach ($this->joinWith as $config) {
457 51
            list($with, $eagerLoading, $joinType) = $config;
458 51
            $this->joinWithRelations($model, $with, $joinType);
0 ignored issues
show
Compatibility introduced by
$model of type object<yii\db\ActiveRecordInterface> is not a sub-type of object<yii\db\ActiveRecord>. It seems like you assume a concrete implementation of the interface yii\db\ActiveRecordInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
459
460 51
            if (is_array($eagerLoading)) {
461
                foreach ($with as $name => $callback) {
462
                    if (is_int($name)) {
463
                        if (!in_array($callback, $eagerLoading, true)) {
464
                            unset($with[$name]);
465
                        }
466
                    } elseif (!in_array($name, $eagerLoading, true)) {
467
                        unset($with[$name]);
468
                    }
469
                }
470 51
            } elseif (!$eagerLoading) {
471 15
                $with = [];
472
            }
473
474 51
            $this->with($with);
475
        }
476
477
        // remove duplicated joins added by joinWithRelations that may be added
478
        // e.g. when joining a relation and a via relation at the same time
479 51
        $uniqueJoins = [];
480 51
        foreach ($this->join as $j) {
481 51
            $uniqueJoins[serialize($j)] = $j;
482
        }
483 51
        $this->join = array_values($uniqueJoins);
484
485 51
        if (!empty($join)) {
486
            // append explicit join to joinWith()
487
            // https://github.com/yiisoft/yii2/issues/2880
488
            $this->join = empty($this->join) ? $join : array_merge($this->join, $join);
489
        }
490 51
    }
491
492
    /**
493
     * Inner joins with the specified relations.
494
     * This is a shortcut method to [[joinWith()]] with the join type set as "INNER JOIN".
495
     * Please refer to [[joinWith()]] for detailed usage of this method.
496
     * @param string|array $with the relations to be joined with.
497
     * @param bool|array $eagerLoading whether to eager load the relations.
498
     * Note, that this does not mean, that the relations are populated from the
499
     * query result. An extra query will still be performed to bring in the
500
     * related data.
501
     * @return $this the query object itself
502
     * @see joinWith()
503
     */
504 21
    public function innerJoinWith($with, $eagerLoading = true)
505
    {
506 21
        return $this->joinWith($with, $eagerLoading, 'INNER JOIN');
507
    }
508
509
    /**
510
     * Modifies the current query by adding join fragments based on the given relations.
511
     * @param ActiveRecord $model the primary model
512
     * @param array $with the relations to be joined
513
     * @param string|array $joinType the join type
514
     */
515 51
    private function joinWithRelations($model, $with, $joinType)
516
    {
517 51
        $relations = [];
518
519 51
        foreach ($with as $name => $callback) {
520 51
            if (is_int($name)) {
521 48
                $name = $callback;
522 48
                $callback = null;
523
            }
524
525 51
            $primaryModel = $model;
526 51
            $parent = $this;
527 51
            $prefix = '';
528 51
            while (($pos = strpos($name, '.')) !== false) {
529 6
                $childName = substr($name, $pos + 1);
530 6
                $name = substr($name, 0, $pos);
531 6
                $fullName = $prefix === '' ? $name : "$prefix.$name";
532 6
                if (!isset($relations[$fullName])) {
533
                    $relations[$fullName] = $relation = $primaryModel->getRelation($name);
534
                    $this->joinWithRelation($parent, $relation, $this->getJoinType($joinType, $fullName));
0 ignored issues
show
Compatibility introduced by
$relation of type object<yii\db\ActiveQueryInterface> is not a sub-type of object<yii\db\ActiveQuery>. It seems like you assume a concrete implementation of the interface yii\db\ActiveQueryInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
535
                } else {
536 6
                    $relation = $relations[$fullName];
537
                }
538
                /* @var $relationModelClass ActiveRecordInterface */
539 6
                $relationModelClass = $relation->modelClass;
540 6
                $primaryModel = $relationModelClass::instance();
541 6
                $parent = $relation;
542 6
                $prefix = $fullName;
543 6
                $name = $childName;
544
            }
545
546 51
            $fullName = $prefix === '' ? $name : "$prefix.$name";
547 51
            if (!isset($relations[$fullName])) {
548 51
                $relations[$fullName] = $relation = $primaryModel->getRelation($name);
549 51
                if ($callback !== null) {
550 18
                    call_user_func($callback, $relation);
551
                }
552 51
                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?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
553 6
                    $relation->buildJoinWith();
554
                }
555 51
                $this->joinWithRelation($parent, $relation, $this->getJoinType($joinType, $fullName));
0 ignored issues
show
Compatibility introduced by
$relation of type object<yii\db\ActiveQueryInterface> is not a sub-type of object<yii\db\ActiveQuery>. It seems like you assume a concrete implementation of the interface yii\db\ActiveQueryInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
556
            }
557
        }
558 51
    }
559
560
    /**
561
     * Returns the join type based on the given join type parameter and the relation name.
562
     * @param string|array $joinType the given join type(s)
563
     * @param string $name relation name
564
     * @return string the real join type
565
     */
566 51
    private function getJoinType($joinType, $name)
567
    {
568 51
        if (is_array($joinType) && isset($joinType[$name])) {
569
            return $joinType[$name];
570
        }
571
572 51
        return is_string($joinType) ? $joinType : 'INNER JOIN';
573
    }
574
575
    /**
576
     * Returns the table name and the table alias for [[modelClass]].
577
     * @return array the table name and the table alias.
578
     * @since 2.0.16
579
     */
580 69
    protected function getTableNameAndAlias()
581
    {
582 69
        if (empty($this->from)) {
583 63
            $tableName = $this->getPrimaryTableName();
584
        } else {
585 54
            $tableName = '';
586
            // if the first entry in "from" is an alias-tablename-pair return it directly
587 54
            foreach ($this->from as $alias => $tableName) {
588 54
                if (is_string($alias)) {
589 18
                    return [$tableName, $alias];
590
                }
591 45
                break;
592
            }
593
        }
594
595 66
        if (preg_match('/^(.*?)\s+({{\w+}}|\w+)$/', $tableName, $matches)) {
596 3
            $alias = $matches[2];
597
        } else {
598 66
            $alias = $tableName;
599
        }
600
601 66
        return [$tableName, $alias];
602
    }
603
604
    /**
605
     * Joins a parent query with a child query.
606
     * The current query object will be modified accordingly.
607
     * @param ActiveQuery $parent
608
     * @param ActiveQuery $child
609
     * @param string $joinType
610
     */
611 51
    private function joinWithRelation($parent, $child, $joinType)
612
    {
613 51
        $via = $child->via;
614 51
        $child->via = null;
615 51
        if ($via instanceof self) {
616
            // via table
617 9
            $this->joinWithRelation($parent, $via, $joinType);
618 9
            $this->joinWithRelation($via, $child, $joinType);
619 9
            return;
620 51
        } elseif (is_array($via)) {
621
            // via relation
622 15
            $this->joinWithRelation($parent, $via[1], $joinType);
623 15
            $this->joinWithRelation($via[1], $child, $joinType);
624 15
            return;
625
        }
626
627 51
        list($parentTable, $parentAlias) = $parent->getTableNameAndAlias();
0 ignored issues
show
Unused Code introduced by
The assignment to $parentTable is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
628 51
        list($childTable, $childAlias) = $child->getTableNameAndAlias();
629
630 51
        if (!empty($child->link)) {
631 51
            if (strpos($parentAlias, '{{') === false) {
632 45
                $parentAlias = '{{' . $parentAlias . '}}';
633
            }
634 51
            if (strpos($childAlias, '{{') === false) {
635 51
                $childAlias = '{{' . $childAlias . '}}';
636
            }
637
638 51
            $on = [];
639 51
            foreach ($child->link as $childColumn => $parentColumn) {
640 51
                $on[] = "$parentAlias.[[$parentColumn]] = $childAlias.[[$childColumn]]";
641
            }
642 51
            $on = implode(' AND ', $on);
643 51
            if (!empty($child->on)) {
644 51
                $on = ['and', $on, $child->on];
645
            }
646
        } else {
647
            $on = $child->on;
648
        }
649 51
        $this->join($joinType, empty($child->from) ? $childTable : $child->from, $on);
650
651 51
        if (!empty($child->where)) {
652 6
            $this->andWhere($child->where);
653
        }
654 51
        if (!empty($child->having)) {
655
            $this->andHaving($child->having);
656
        }
657 51
        if (!empty($child->orderBy)) {
658 15
            $this->addOrderBy($child->orderBy);
659
        }
660 51
        if (!empty($child->groupBy)) {
661
            $this->addGroupBy($child->groupBy);
662
        }
663 51
        if (!empty($child->params)) {
664
            $this->addParams($child->params);
665
        }
666 51
        if (!empty($child->join)) {
667 6
            foreach ($child->join as $join) {
668 6
                $this->join[] = $join;
669
            }
670
        }
671 51
        if (!empty($child->union)) {
672
            foreach ($child->union as $union) {
673
                $this->union[] = $union;
674
            }
675
        }
676 51
    }
677
678
    /**
679
     * Sets the ON condition for a relational query.
680
     * The condition will be used in the ON part when [[ActiveQuery::joinWith()]] is called.
681
     * Otherwise, the condition will be used in the WHERE part of a query.
682
     *
683
     * Use this method to specify additional conditions when declaring a relation in the [[ActiveRecord]] class:
684
     *
685
     * ```php
686
     * public function getActiveUsers()
687
     * {
688
     *     return $this->hasMany(User::className(), ['id' => 'user_id'])
689
     *                 ->onCondition(['active' => true]);
690
     * }
691
     * ```
692
     *
693
     * Note that this condition is applied in case of a join as well as when fetching the related records.
694
     * Thus only fields of the related table can be used in the condition. Trying to access fields of the primary
695
     * record will cause an error in a non-join-query.
696
     *
697
     * @param string|array $condition the ON condition. Please refer to [[Query::where()]] on how to specify this parameter.
698
     * @param array $params the parameters (name => value) to be bound to the query.
699
     * @return $this the query object itself
700
     */
701 21
    public function onCondition($condition, $params = [])
702
    {
703 21
        $this->on = $condition;
704 21
        $this->addParams($params);
705 21
        return $this;
706
    }
707
708
    /**
709
     * Adds an additional ON condition to the existing one.
710
     * The new condition and the existing one will be joined using the 'AND' operator.
711
     * @param string|array $condition the new ON condition. Please refer to [[where()]]
712
     * on how to specify this parameter.
713
     * @param array $params the parameters (name => value) to be bound to the query.
714
     * @return $this the query object itself
715
     * @see onCondition()
716
     * @see orOnCondition()
717
     */
718 6
    public function andOnCondition($condition, $params = [])
719
    {
720 6
        if ($this->on === null) {
721 3
            $this->on = $condition;
722
        } else {
723 3
            $this->on = ['and', $this->on, $condition];
724
        }
725 6
        $this->addParams($params);
726 6
        return $this;
727
    }
728
729
    /**
730
     * Adds an additional ON condition to the existing one.
731
     * The new condition and the existing one will be joined using the 'OR' operator.
732
     * @param string|array $condition the new ON condition. Please refer to [[where()]]
733
     * on how to specify this parameter.
734
     * @param array $params the parameters (name => value) to be bound to the query.
735
     * @return $this the query object itself
736
     * @see onCondition()
737
     * @see andOnCondition()
738
     */
739 6
    public function orOnCondition($condition, $params = [])
740
    {
741 6
        if ($this->on === null) {
742 3
            $this->on = $condition;
743
        } else {
744 3
            $this->on = ['or', $this->on, $condition];
745
        }
746 6
        $this->addParams($params);
747 6
        return $this;
748
    }
749
750
    /**
751
     * Specifies the junction table for a relational query.
752
     *
753
     * Use this method to specify a junction table when declaring a relation in the [[ActiveRecord]] class:
754
     *
755
     * ```php
756
     * public function getItems()
757
     * {
758
     *     return $this->hasMany(Item::className(), ['id' => 'item_id'])
759
     *                 ->viaTable('order_item', ['order_id' => 'id']);
760
     * }
761
     * ```
762
     *
763
     * @param string $tableName the name of the junction table.
764
     * @param array $link the link between the junction table and the table associated with [[primaryModel]].
765
     * The keys of the array represent the columns in the junction table, and the values represent the columns
766
     * in the [[primaryModel]] table.
767
     * @param callable $callable a PHP callback for customizing the relation associated with the junction table.
768
     * Its signature should be `function($query)`, where `$query` is the query to be customized.
769
     * @return $this the query object itself
770
     * @throws InvalidConfigException when query is not initialized properly
771
     * @see via()
772
     */
773 27
    public function viaTable($tableName, $link, callable $callable = null)
774
    {
775 27
        $modelClass = $this->primaryModel ? get_class($this->primaryModel) : $this->modelClass;
776 27
        $relation = new self($modelClass, [
777 27
            'from' => [$tableName],
778 27
            'link' => $link,
779
            'multiple' => true,
780
            'asArray' => true,
781
        ]);
782 27
        $this->via = $relation;
783 27
        if ($callable !== null) {
784 6
            call_user_func($callable, $relation);
785
        }
786
787 27
        return $this;
788
    }
789
790
    /**
791
     * Define an alias for the table defined in [[modelClass]].
792
     *
793
     * This method will adjust [[from]] so that an already defined alias will be overwritten.
794
     * If none was defined, [[from]] will be populated with the given alias.
795
     *
796
     * @param string $alias the table alias.
797
     * @return $this the query object itself
798
     * @since 2.0.7
799
     */
800 24
    public function alias($alias)
801
    {
802 24
        if (empty($this->from) || count($this->from) < 2) {
803 24
            list($tableName) = $this->getTableNameAndAlias();
804 24
            $this->from = [$alias => $tableName];
805
        } else {
806 3
            $tableName = $this->getPrimaryTableName();
807
808 3
            foreach ($this->from as $key => $table) {
809 3
                if ($table === $tableName) {
810 3
                    unset($this->from[$key]);
811 3
                    $this->from[$alias] = $tableName;
812
                }
813
            }
814
        }
815
816 24
        return $this;
817
    }
818
819
    /**
820
     * {@inheritdoc}
821
     * @since 2.0.12
822
     */
823 136
    public function getTablesUsedInFrom()
824
    {
825 136
        if (empty($this->from)) {
826 103
            return $this->cleanUpTableNames([$this->getPrimaryTableName()]);
827
        }
828
829 33
        return parent::getTablesUsedInFrom();
830
    }
831
832
    /**
833
     * @return string primary table name
834
     * @since 2.0.12
835
     */
836 421
    protected function getPrimaryTableName()
837
    {
838
        /* @var $modelClass ActiveRecord */
839 421
        $modelClass = $this->modelClass;
840 421
        return $modelClass::tableName();
841
    }
842
}
843