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
|
476 |
|
public function __construct($modelClass, $config = []) |
108
|
|
|
{ |
109
|
476 |
|
$this->modelClass = $modelClass; |
110
|
476 |
|
parent::__construct($config); |
111
|
476 |
|
} |
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
|
476 |
|
public function init() |
120
|
|
|
{ |
121
|
476 |
|
parent::init(); |
122
|
476 |
|
$this->trigger(self::EVENT_INIT); |
123
|
476 |
|
} |
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
|
195 |
|
public function all($db = null) |
132
|
|
|
{ |
133
|
195 |
|
return parent::all($db); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
384 |
|
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
|
384 |
|
if (!empty($this->joinWith)) { |
146
|
48 |
|
$this->buildJoinWith(); |
147
|
48 |
|
$this->joinWith = null; // clean it up to avoid issue https://github.com/yiisoft/yii2/issues/2687 |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
384 |
|
if (empty($this->from)) { |
151
|
381 |
|
$this->from = [$this->getPrimaryTableName()]; |
152
|
|
|
} |
153
|
|
|
|
154
|
384 |
|
if (empty($this->select) && !empty($this->join)) { |
155
|
39 |
|
[, $alias] = $this->getTableNameAndAlias(); |
|
|
|
|
156
|
39 |
|
$this->select = ["$alias.*"]; |
157
|
|
|
} |
158
|
|
|
|
159
|
384 |
|
if ($this->primaryModel === null) { |
160
|
|
|
// eager loading |
161
|
380 |
|
$query = Query::create($this); |
162
|
|
|
} else { |
163
|
|
|
// lazy loading of a relation |
164
|
103 |
|
$where = $this->where; |
165
|
|
|
|
166
|
103 |
|
if ($this->via instanceof self) { |
167
|
|
|
// via junction table |
168
|
15 |
|
$viaModels = $this->via->findJunctionRows([$this->primaryModel]); |
169
|
15 |
|
$this->filterByModels($viaModels); |
170
|
94 |
|
} elseif (is_array($this->via)) { |
171
|
|
|
// via relation |
172
|
|
|
/* @var $viaQuery ActiveQuery */ |
173
|
30 |
|
[$viaName, $viaQuery] = $this->via; |
|
|
|
|
174
|
30 |
|
if ($viaQuery->multiple) { |
175
|
30 |
|
$viaModels = $viaQuery->all(); |
176
|
30 |
|
$this->primaryModel->populateRelation($viaName, $viaModels); |
177
|
|
|
} else { |
178
|
|
|
$model = $viaQuery->one(); |
179
|
|
|
$this->primaryModel->populateRelation($viaName, $model); |
180
|
|
|
$viaModels = $model === null ? [] : [$model]; |
181
|
|
|
} |
182
|
30 |
|
$this->filterByModels($viaModels); |
183
|
|
|
} else { |
184
|
94 |
|
$this->filterByModels([$this->primaryModel]); |
185
|
|
|
} |
186
|
|
|
|
187
|
103 |
|
$query = Query::create($this); |
188
|
103 |
|
$this->where = $where; |
189
|
|
|
} |
190
|
|
|
|
191
|
384 |
|
if (!empty($this->on)) { |
192
|
18 |
|
$query->andWhere($this->on); |
193
|
|
|
} |
194
|
|
|
|
195
|
384 |
|
return $query; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritdoc} |
200
|
|
|
*/ |
201
|
328 |
|
public function populate($rows) |
202
|
|
|
{ |
203
|
328 |
|
if (empty($rows)) { |
204
|
55 |
|
return []; |
205
|
|
|
} |
206
|
|
|
|
207
|
319 |
|
$models = $this->createModels($rows); |
208
|
319 |
|
if (!empty($this->join) && $this->indexBy === null) { |
209
|
33 |
|
$models = $this->removeDuplicatedModels($models); |
210
|
|
|
} |
211
|
319 |
|
if (!empty($this->with)) { |
212
|
84 |
|
$this->findWith($this->with, $models); |
213
|
|
|
} |
214
|
|
|
|
215
|
319 |
|
if ($this->inverseOf !== null) { |
216
|
12 |
|
$this->addInverseRelations($models); |
217
|
|
|
} |
218
|
|
|
|
219
|
319 |
|
if (!$this->asArray) { |
220
|
309 |
|
foreach ($models as $model) { |
221
|
309 |
|
$model->afterFind(); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
319 |
|
return parent::populate($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
|
33 |
|
private function removeDuplicatedModels($models) |
236
|
|
|
{ |
237
|
33 |
|
$hash = []; |
238
|
|
|
/* @var $class ActiveRecord */ |
239
|
33 |
|
$class = $this->modelClass; |
240
|
33 |
|
$pks = $class::primaryKey(); |
241
|
|
|
|
242
|
33 |
|
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
|
30 |
|
$pk = reset($pks); |
265
|
30 |
|
foreach ($models as $i => $model) { |
266
|
30 |
|
if (!isset($model[$pk])) { |
267
|
|
|
// do not continue if the primary key is not part of the result set |
268
|
3 |
|
break; |
269
|
|
|
} |
270
|
27 |
|
$key = $model[$pk]; |
271
|
27 |
|
if (isset($hash[$key])) { |
272
|
12 |
|
unset($models[$i]); |
273
|
27 |
|
} elseif ($key !== null) { |
274
|
27 |
|
$hash[$key] = true; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
33 |
|
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
|
272 |
|
public function one($db = null) |
291
|
|
|
{ |
292
|
272 |
|
$row = parent::one($db); |
293
|
272 |
|
if ($row !== false) { |
294
|
269 |
|
$models = $this->populate([$row]); |
295
|
269 |
|
return reset($models) ?: null; |
296
|
|
|
} |
297
|
|
|
|
298
|
27 |
|
return null; |
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
|
384 |
|
public function createCommand($db = null) |
308
|
|
|
{ |
309
|
|
|
/* @var $modelClass ActiveRecord */ |
310
|
384 |
|
$modelClass = $this->modelClass; |
311
|
384 |
|
if ($db === null) { |
312
|
373 |
|
$db = $modelClass::getDb(); |
313
|
|
|
} |
314
|
|
|
|
315
|
384 |
|
if ($this->sql === null) { |
316
|
381 |
|
[$sql, $params] = $db->getQueryBuilder()->build($this); |
|
|
|
|
317
|
|
|
} else { |
318
|
3 |
|
$sql = $this->sql; |
319
|
3 |
|
$params = $this->params; |
320
|
|
|
} |
321
|
|
|
|
322
|
384 |
|
$command = $db->createCommand($sql, $params); |
|
|
|
|
323
|
384 |
|
$this->setCommandCache($command); |
324
|
|
|
|
325
|
384 |
|
return $command; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* {@inheritdoc} |
330
|
|
|
*/ |
331
|
64 |
|
protected function queryScalar($selectExpression, $db) |
332
|
|
|
{ |
333
|
|
|
/* @var $modelClass ActiveRecord */ |
334
|
64 |
|
$modelClass = $this->modelClass; |
335
|
64 |
|
if ($db === null) { |
336
|
63 |
|
$db = $modelClass::getDb(); |
337
|
|
|
} |
338
|
|
|
|
339
|
64 |
|
if ($this->sql === null) { |
340
|
61 |
|
return parent::queryScalar($selectExpression, $db); |
341
|
|
|
} |
342
|
|
|
|
343
|
3 |
|
$command = (new Query())->select([$selectExpression]) |
344
|
3 |
|
->from(['c' => "({$this->sql})"]) |
345
|
3 |
|
->params($this->params) |
346
|
3 |
|
->createCommand($db); |
347
|
3 |
|
$this->setCommandCache($command); |
348
|
|
|
|
349
|
3 |
|
return $command->queryScalar(); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Joins with the specified relations. |
354
|
|
|
* |
355
|
|
|
* This method allows you to reuse existing relation definitions to perform JOIN queries. |
356
|
|
|
* Based on the definition of the specified relation(s), the method will append one or multiple |
357
|
|
|
* JOIN statements to the current query. |
358
|
|
|
* |
359
|
|
|
* If the `$eagerLoading` parameter is true, the method will also perform eager loading for the specified relations, |
360
|
|
|
* which is equivalent to calling [[with()]] using the specified relations. |
361
|
|
|
* |
362
|
|
|
* Note that because a JOIN query will be performed, you are responsible to disambiguate column names. |
363
|
|
|
* |
364
|
|
|
* This method differs from [[with()]] in that it will build up and execute a JOIN SQL statement |
365
|
|
|
* for the primary table. And when `$eagerLoading` is true, it will call [[with()]] in addition with the specified relations. |
366
|
|
|
* |
367
|
|
|
* @param string|array $with the relations to be joined. This can either be a string, representing a relation name or |
368
|
|
|
* an array with the following semantics: |
369
|
|
|
* |
370
|
|
|
* - Each array element represents a single relation. |
371
|
|
|
* - You may specify the relation name as the array key and provide an anonymous functions that |
372
|
|
|
* can be used to modify the relation queries on-the-fly as the array value. |
373
|
|
|
* - If a relation query does not need modification, you may use the relation name as the array value. |
374
|
|
|
* |
375
|
|
|
* The relation name may optionally contain an alias for the relation table (e.g. `books b`). |
376
|
|
|
* |
377
|
|
|
* Sub-relations can also be specified, see [[with()]] for the syntax. |
378
|
|
|
* |
379
|
|
|
* In the following you find some examples: |
380
|
|
|
* |
381
|
|
|
* ```php |
382
|
|
|
* // find all orders that contain books, and eager loading "books" |
383
|
|
|
* Order::find()->joinWith('books', true, 'INNER JOIN')->all(); |
384
|
|
|
* // find all orders, eager loading "books", and sort the orders and books by the book names. |
385
|
|
|
* Order::find()->joinWith([ |
386
|
|
|
* 'books' => function (\yii\db\ActiveQuery $query) { |
387
|
|
|
* $query->orderBy('item.name'); |
388
|
|
|
* } |
389
|
|
|
* ])->all(); |
390
|
|
|
* // find all orders that contain books of the category 'Science fiction', using the alias "b" for the books table |
391
|
|
|
* Order::find()->joinWith(['books b'], true, 'INNER JOIN')->where(['b.category' => 'Science fiction'])->all(); |
392
|
|
|
* ``` |
393
|
|
|
* |
394
|
|
|
* The alias syntax is available since version 2.0.7. |
395
|
|
|
* |
396
|
|
|
* @param bool|array $eagerLoading whether to eager load the relations |
397
|
|
|
* specified in `$with`. When this is a boolean, it applies to all |
398
|
|
|
* relations specified in `$with`. Use an array to explicitly list which |
399
|
|
|
* relations in `$with` need to be eagerly loaded. Note, that this does |
400
|
|
|
* not mean, that the relations are populated from the query result. An |
401
|
|
|
* extra query will still be performed to bring in the related data. |
402
|
|
|
* Defaults to `true`. |
403
|
|
|
* @param string|array $joinType the join type of the relations specified in `$with`. |
404
|
|
|
* When this is a string, it applies to all relations specified in `$with`. Use an array |
405
|
|
|
* in the format of `relationName => joinType` to specify different join types for different relations. |
406
|
|
|
* @return $this the query object itself |
407
|
|
|
*/ |
408
|
54 |
|
public function joinWith($with, $eagerLoading = true, $joinType = 'LEFT JOIN') |
409
|
|
|
{ |
410
|
54 |
|
$relations = []; |
411
|
54 |
|
foreach ((array) $with as $name => $callback) { |
412
|
54 |
|
if (is_int($name)) { |
413
|
54 |
|
$name = $callback; |
414
|
54 |
|
$callback = null; |
415
|
|
|
} |
416
|
|
|
|
417
|
54 |
|
if (preg_match('/^(.*?)(?:\s+AS\s+|\s+)(\w+)$/i', $name, $matches)) { |
418
|
|
|
// relation is defined with an alias, adjust callback to apply alias |
419
|
12 |
|
[, $relation, $alias] = $matches; |
|
|
|
|
420
|
12 |
|
$name = $relation; |
|
|
|
|
421
|
12 |
|
$callback = function ($query) use ($callback, $alias) { |
422
|
|
|
/* @var $query ActiveQuery */ |
423
|
12 |
|
$query->alias($alias); |
424
|
12 |
|
if ($callback !== null) { |
425
|
9 |
|
call_user_func($callback, $query); |
426
|
|
|
} |
427
|
12 |
|
}; |
428
|
|
|
} |
429
|
|
|
|
430
|
54 |
|
if ($callback === null) { |
431
|
51 |
|
$relations[] = $name; |
432
|
|
|
} else { |
433
|
54 |
|
$relations[$name] = $callback; |
434
|
|
|
} |
435
|
|
|
} |
436
|
54 |
|
$this->joinWith[] = [$relations, $eagerLoading, $joinType]; |
437
|
54 |
|
return $this; |
438
|
|
|
} |
439
|
|
|
|
440
|
48 |
|
private function buildJoinWith() |
441
|
|
|
{ |
442
|
48 |
|
$join = $this->join; |
443
|
48 |
|
$this->join = []; |
444
|
|
|
|
445
|
|
|
/* @var $modelClass ActiveRecordInterface */ |
446
|
48 |
|
$modelClass = $this->modelClass; |
447
|
48 |
|
$model = $modelClass::instance(); |
448
|
48 |
|
foreach ($this->joinWith as [$with, $eagerLoading, $joinType]) { |
449
|
48 |
|
$this->joinWithRelations($model, $with, $joinType); |
|
|
|
|
450
|
|
|
|
451
|
48 |
|
if (is_array($eagerLoading)) { |
452
|
|
|
foreach ($with as $name => $callback) { |
453
|
|
|
if (is_int($name)) { |
454
|
|
|
if (!in_array($callback, $eagerLoading, true)) { |
|
|
|
|
455
|
|
|
unset($with[$name]); |
456
|
|
|
} |
457
|
|
|
} elseif (!in_array($name, $eagerLoading, true)) { |
458
|
|
|
unset($with[$name]); |
459
|
|
|
} |
460
|
|
|
} |
461
|
48 |
|
} elseif (!$eagerLoading) { |
462
|
15 |
|
$with = []; |
463
|
|
|
} |
464
|
|
|
|
465
|
48 |
|
$this->with($with); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
// remove duplicated joins added by joinWithRelations that may be added |
469
|
|
|
// e.g. when joining a relation and a via relation at the same time |
470
|
48 |
|
$uniqueJoins = []; |
471
|
48 |
|
foreach ($this->join as $j) { |
472
|
48 |
|
$uniqueJoins[serialize($j)] = $j; |
473
|
|
|
} |
474
|
48 |
|
$this->join = array_values($uniqueJoins); |
475
|
|
|
|
476
|
48 |
|
if (!empty($join)) { |
477
|
|
|
// append explicit join to joinWith() |
478
|
|
|
// https://github.com/yiisoft/yii2/issues/2880 |
479
|
|
|
$this->join = empty($this->join) ? $join : array_merge($this->join, $join); |
480
|
|
|
} |
481
|
48 |
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Inner joins with the specified relations. |
485
|
|
|
* This is a shortcut method to [[joinWith()]] with the join type set as "INNER JOIN". |
486
|
|
|
* Please refer to [[joinWith()]] for detailed usage of this method. |
487
|
|
|
* @param string|array $with the relations to be joined with. |
488
|
|
|
* @param bool|array $eagerLoading whether to eager load the relations. |
489
|
|
|
* Note, that this does not mean, that the relations are populated from the |
490
|
|
|
* query result. An extra query will still be performed to bring in the |
491
|
|
|
* related data. |
492
|
|
|
* @return $this the query object itself |
493
|
|
|
* @see joinWith() |
494
|
|
|
*/ |
495
|
18 |
|
public function innerJoinWith($with, $eagerLoading = true) |
496
|
|
|
{ |
497
|
18 |
|
return $this->joinWith($with, $eagerLoading, 'INNER JOIN'); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* Modifies the current query by adding join fragments based on the given relations. |
502
|
|
|
* @param ActiveRecord $model the primary model |
503
|
|
|
* @param array $with the relations to be joined |
504
|
|
|
* @param string|array $joinType the join type |
505
|
|
|
*/ |
506
|
48 |
|
private function joinWithRelations($model, $with, $joinType) |
507
|
|
|
{ |
508
|
48 |
|
$relations = []; |
509
|
|
|
|
510
|
48 |
|
foreach ($with as $name => $callback) { |
511
|
48 |
|
if (is_int($name)) { |
512
|
45 |
|
$name = $callback; |
513
|
45 |
|
$callback = null; |
514
|
|
|
} |
515
|
|
|
|
516
|
48 |
|
$primaryModel = $model; |
517
|
48 |
|
$parent = $this; |
518
|
48 |
|
$prefix = ''; |
519
|
48 |
|
while (($pos = strpos($name, '.')) !== false) { |
520
|
6 |
|
$childName = substr($name, $pos + 1); |
521
|
6 |
|
$name = substr($name, 0, $pos); |
522
|
6 |
|
$fullName = $prefix === '' ? $name : "$prefix.$name"; |
523
|
6 |
|
if (!isset($relations[$fullName])) { |
524
|
|
|
$relations[$fullName] = $relation = $primaryModel->getRelation($name); |
525
|
|
|
$this->joinWithRelation($parent, $relation, $this->getJoinType($joinType, $fullName)); |
|
|
|
|
526
|
|
|
} else { |
527
|
6 |
|
$relation = $relations[$fullName]; |
528
|
|
|
} |
529
|
|
|
/* @var $relationModelClass ActiveRecordInterface */ |
530
|
6 |
|
$relationModelClass = $relation->modelClass; |
531
|
6 |
|
$primaryModel = $relationModelClass::instance(); |
532
|
6 |
|
$parent = $relation; |
533
|
6 |
|
$prefix = $fullName; |
534
|
6 |
|
$name = $childName; |
535
|
|
|
} |
536
|
|
|
|
537
|
48 |
|
$fullName = $prefix === '' ? $name : "$prefix.$name"; |
538
|
48 |
|
if (!isset($relations[$fullName])) { |
539
|
48 |
|
$relations[$fullName] = $relation = $primaryModel->getRelation($name); |
540
|
48 |
|
if ($callback !== null) { |
541
|
18 |
|
call_user_func($callback, $relation); |
542
|
|
|
} |
543
|
48 |
|
if (!empty($relation->joinWith)) { |
|
|
|
|
544
|
6 |
|
$relation->buildJoinWith(); |
545
|
|
|
} |
546
|
48 |
|
$this->joinWithRelation($parent, $relation, $this->getJoinType($joinType, $fullName)); |
|
|
|
|
547
|
|
|
} |
548
|
|
|
} |
549
|
48 |
|
} |
550
|
|
|
|
551
|
|
|
/** |
552
|
|
|
* Returns the join type based on the given join type parameter and the relation name. |
553
|
|
|
* @param string|array $joinType the given join type(s) |
554
|
|
|
* @param string $name relation name |
555
|
|
|
* @return string the real join type |
556
|
|
|
*/ |
557
|
48 |
|
private function getJoinType($joinType, $name) |
558
|
|
|
{ |
559
|
48 |
|
if (is_array($joinType) && isset($joinType[$name])) { |
560
|
|
|
return $joinType[$name]; |
561
|
|
|
} |
562
|
|
|
|
563
|
48 |
|
return is_string($joinType) ? $joinType : 'INNER JOIN'; |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* Returns the table name and the table alias for [[modelClass]]. |
568
|
|
|
* @return array the table name and the table alias. |
569
|
|
|
* @internal |
570
|
|
|
*/ |
571
|
66 |
|
private function getTableNameAndAlias() |
572
|
|
|
{ |
573
|
66 |
|
if (empty($this->from)) { |
574
|
60 |
|
$tableName = $this->getPrimaryTableName(); |
575
|
|
|
} else { |
576
|
51 |
|
$tableName = ''; |
577
|
51 |
|
foreach ($this->from as $alias => $tableName) { |
578
|
51 |
|
if (is_string($alias)) { |
579
|
18 |
|
return [$tableName, $alias]; |
580
|
|
|
} |
581
|
42 |
|
break; |
582
|
|
|
} |
583
|
|
|
} |
584
|
|
|
|
585
|
63 |
|
if (preg_match('/^(.*?)\s+({{\w+}}|\w+)$/', $tableName, $matches)) { |
586
|
3 |
|
$alias = $matches[2]; |
587
|
|
|
} else { |
588
|
63 |
|
$alias = $tableName; |
589
|
|
|
} |
590
|
|
|
|
591
|
63 |
|
return [$tableName, $alias]; |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
/** |
595
|
|
|
* Joins a parent query with a child query. |
596
|
|
|
* The current query object will be modified accordingly. |
597
|
|
|
* @param ActiveQuery $parent |
598
|
|
|
* @param ActiveQuery $child |
599
|
|
|
* @param string $joinType |
600
|
|
|
*/ |
601
|
48 |
|
private function joinWithRelation($parent, $child, $joinType) |
602
|
|
|
{ |
603
|
48 |
|
$via = $child->via; |
604
|
48 |
|
$child->via = null; |
605
|
48 |
|
if ($via instanceof self) { |
606
|
|
|
// via table |
607
|
9 |
|
$this->joinWithRelation($parent, $via, $joinType); |
608
|
9 |
|
$this->joinWithRelation($via, $child, $joinType); |
609
|
9 |
|
return; |
610
|
48 |
|
} elseif (is_array($via)) { |
611
|
|
|
// via relation |
612
|
15 |
|
$this->joinWithRelation($parent, $via[1], $joinType); |
613
|
15 |
|
$this->joinWithRelation($via[1], $child, $joinType); |
614
|
15 |
|
return; |
615
|
|
|
} |
616
|
|
|
|
617
|
48 |
|
[$parentTable, $parentAlias] = $parent->getTableNameAndAlias(); |
|
|
|
|
618
|
48 |
|
[$childTable, $childAlias] = $child->getTableNameAndAlias(); |
|
|
|
|
619
|
|
|
|
620
|
48 |
|
if (!empty($child->link)) { |
621
|
48 |
|
if (strpos($parentAlias, '{{') === false) { |
|
|
|
|
622
|
42 |
|
$parentAlias = '{{' . $parentAlias . '}}'; |
|
|
|
|
623
|
|
|
} |
624
|
48 |
|
if (strpos($childAlias, '{{') === false) { |
|
|
|
|
625
|
48 |
|
$childAlias = '{{' . $childAlias . '}}'; |
|
|
|
|
626
|
|
|
} |
627
|
|
|
|
628
|
48 |
|
$on = []; |
629
|
48 |
|
foreach ($child->link as $childColumn => $parentColumn) { |
630
|
48 |
|
$on[] = "$parentAlias.[[$parentColumn]] = $childAlias.[[$childColumn]]"; |
|
|
|
|
631
|
|
|
} |
632
|
48 |
|
$on = implode(' AND ', $on); |
633
|
48 |
|
if (!empty($child->on)) { |
634
|
48 |
|
$on = ['and', $on, $child->on]; |
635
|
|
|
} |
636
|
|
|
} else { |
637
|
|
|
$on = $child->on; |
638
|
|
|
} |
639
|
48 |
|
$this->join($joinType, empty($child->from) ? $childTable : $child->from, $on); |
640
|
|
|
|
641
|
48 |
|
if (!empty($child->where)) { |
642
|
6 |
|
$this->andWhere($child->where); |
643
|
|
|
} |
644
|
48 |
|
if (!empty($child->having)) { |
645
|
|
|
$this->andHaving($child->having); |
646
|
|
|
} |
647
|
48 |
|
if (!empty($child->orderBy)) { |
648
|
15 |
|
$this->addOrderBy($child->orderBy); |
649
|
|
|
} |
650
|
48 |
|
if (!empty($child->groupBy)) { |
651
|
|
|
$this->addGroupBy($child->groupBy); |
652
|
|
|
} |
653
|
48 |
|
if (!empty($child->params)) { |
654
|
|
|
$this->addParams($child->params); |
655
|
|
|
} |
656
|
48 |
|
if (!empty($child->join)) { |
657
|
6 |
|
foreach ($child->join as $join) { |
658
|
6 |
|
$this->join[] = $join; |
659
|
|
|
} |
660
|
|
|
} |
661
|
48 |
|
if (!empty($child->union)) { |
662
|
|
|
foreach ($child->union as $union) { |
663
|
|
|
$this->union[] = $union; |
664
|
|
|
} |
665
|
|
|
} |
666
|
48 |
|
} |
667
|
|
|
|
668
|
|
|
/** |
669
|
|
|
* Sets the ON condition for a relational query. |
670
|
|
|
* The condition will be used in the ON part when [[ActiveQuery::joinWith()]] is called. |
671
|
|
|
* Otherwise, the condition will be used in the WHERE part of a query. |
672
|
|
|
* |
673
|
|
|
* Use this method to specify additional conditions when declaring a relation in the [[ActiveRecord]] class: |
674
|
|
|
* |
675
|
|
|
* ```php |
676
|
|
|
* public function getActiveUsers() |
677
|
|
|
* { |
678
|
|
|
* return $this->hasMany(User::class, ['id' => 'user_id']) |
679
|
|
|
* ->onCondition(['active' => true]); |
680
|
|
|
* } |
681
|
|
|
* ``` |
682
|
|
|
* |
683
|
|
|
* Note that this condition is applied in case of a join as well as when fetching the related records. |
684
|
|
|
* Thus only fields of the related table can be used in the condition. Trying to access fields of the primary |
685
|
|
|
* record will cause an error in a non-join-query. |
686
|
|
|
* |
687
|
|
|
* @param string|array $condition the ON condition. Please refer to [[Query::where()]] on how to specify this parameter. |
688
|
|
|
* @param array $params the parameters (name => value) to be bound to the query. |
689
|
|
|
* @return $this the query object itself |
690
|
|
|
*/ |
691
|
21 |
|
public function onCondition($condition, $params = []) |
692
|
|
|
{ |
693
|
21 |
|
$this->on = $condition; |
694
|
21 |
|
$this->addParams($params); |
695
|
21 |
|
return $this; |
696
|
|
|
} |
697
|
|
|
|
698
|
|
|
/** |
699
|
|
|
* Adds an additional ON condition to the existing one. |
700
|
|
|
* The new condition and the existing one will be joined using the 'AND' operator. |
701
|
|
|
* @param string|array $condition the new ON condition. Please refer to [[where()]] |
702
|
|
|
* on how to specify this parameter. |
703
|
|
|
* @param array $params the parameters (name => value) to be bound to the query. |
704
|
|
|
* @return $this the query object itself |
705
|
|
|
* @see onCondition() |
706
|
|
|
* @see orOnCondition() |
707
|
|
|
*/ |
708
|
6 |
|
public function andOnCondition($condition, $params = []) |
709
|
|
|
{ |
710
|
6 |
|
if ($this->on === null) { |
711
|
3 |
|
$this->on = $condition; |
712
|
|
|
} else { |
713
|
3 |
|
$this->on = ['and', $this->on, $condition]; |
714
|
|
|
} |
715
|
6 |
|
$this->addParams($params); |
716
|
6 |
|
return $this; |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
/** |
720
|
|
|
* Adds an additional ON condition to the existing one. |
721
|
|
|
* The new condition and the existing one will be joined using the 'OR' operator. |
722
|
|
|
* @param string|array $condition the new ON condition. Please refer to [[where()]] |
723
|
|
|
* on how to specify this parameter. |
724
|
|
|
* @param array $params the parameters (name => value) to be bound to the query. |
725
|
|
|
* @return $this the query object itself |
726
|
|
|
* @see onCondition() |
727
|
|
|
* @see andOnCondition() |
728
|
|
|
*/ |
729
|
6 |
|
public function orOnCondition($condition, $params = []) |
730
|
|
|
{ |
731
|
6 |
|
if ($this->on === null) { |
732
|
3 |
|
$this->on = $condition; |
733
|
|
|
} else { |
734
|
3 |
|
$this->on = ['or', $this->on, $condition]; |
735
|
|
|
} |
736
|
6 |
|
$this->addParams($params); |
737
|
6 |
|
return $this; |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
/** |
741
|
|
|
* Specifies the junction table for a relational query. |
742
|
|
|
* |
743
|
|
|
* Use this method to specify a junction table when declaring a relation in the [[ActiveRecord]] class: |
744
|
|
|
* |
745
|
|
|
* ```php |
746
|
|
|
* public function getItems() |
747
|
|
|
* { |
748
|
|
|
* return $this->hasMany(Item::class, ['id' => 'item_id']) |
749
|
|
|
* ->viaTable('order_item', ['order_id' => 'id']); |
750
|
|
|
* } |
751
|
|
|
* ``` |
752
|
|
|
* |
753
|
|
|
* @param string $tableName the name of the junction table. |
754
|
|
|
* @param array $link the link between the junction table and the table associated with [[primaryModel]]. |
755
|
|
|
* The keys of the array represent the columns in the junction table, and the values represent the columns |
756
|
|
|
* in the [[primaryModel]] table. |
757
|
|
|
* @param callable $callable a PHP callback for customizing the relation associated with the junction table. |
758
|
|
|
* Its signature should be `function($query)`, where `$query` is the query to be customized. |
759
|
|
|
* @return $this the query object itself |
760
|
|
|
* @see via() |
761
|
|
|
*/ |
762
|
24 |
|
public function viaTable($tableName, $link, callable $callable = null) |
763
|
|
|
{ |
764
|
24 |
|
$modelClass = $this->primaryModel !== null ? get_class($this->primaryModel) : get_class(); |
765
|
|
|
|
766
|
24 |
|
$relation = new self($modelClass, [ |
767
|
24 |
|
'from' => [$tableName], |
768
|
24 |
|
'link' => $link, |
769
|
|
|
'multiple' => true, |
770
|
|
|
'asArray' => true, |
771
|
|
|
]); |
772
|
24 |
|
$this->via = $relation; |
773
|
24 |
|
if ($callable !== null) { |
774
|
6 |
|
call_user_func($callable, $relation); |
775
|
|
|
} |
776
|
|
|
|
777
|
24 |
|
return $this; |
778
|
|
|
} |
779
|
|
|
|
780
|
|
|
/** |
781
|
|
|
* Define an alias for the table defined in [[modelClass]]. |
782
|
|
|
* |
783
|
|
|
* This method will adjust [[from]] so that an already defined alias will be overwritten. |
784
|
|
|
* If none was defined, [[from]] will be populated with the given alias. |
785
|
|
|
* |
786
|
|
|
* @param string $alias the table alias. |
787
|
|
|
* @return $this the query object itself |
788
|
|
|
* @since 2.0.7 |
789
|
|
|
*/ |
790
|
24 |
|
public function alias($alias) |
791
|
|
|
{ |
792
|
24 |
|
if (empty($this->from) || count($this->from) < 2) { |
793
|
24 |
|
[$tableName] = $this->getTableNameAndAlias(); |
|
|
|
|
794
|
24 |
|
$this->from = [$alias => $tableName]; |
|
|
|
|
795
|
|
|
} else { |
796
|
3 |
|
$tableName = $this->getPrimaryTableName(); |
797
|
|
|
|
798
|
3 |
|
foreach ($this->from as $key => $table) { |
799
|
3 |
|
if ($table === $tableName) { |
800
|
3 |
|
unset($this->from[$key]); |
801
|
3 |
|
$this->from[$alias] = $tableName; |
802
|
|
|
} |
803
|
|
|
} |
804
|
|
|
} |
805
|
|
|
|
806
|
24 |
|
return $this; |
807
|
|
|
} |
808
|
|
|
|
809
|
|
|
/** |
810
|
|
|
* {@inheritdoc} |
811
|
|
|
* @since 2.0.12 |
812
|
|
|
*/ |
813
|
135 |
|
public function getTablesUsedInFrom() |
814
|
|
|
{ |
815
|
135 |
|
if (empty($this->from)) { |
816
|
102 |
|
return $this->cleanUpTableNames([$this->getPrimaryTableName()]); |
817
|
|
|
} |
818
|
|
|
|
819
|
33 |
|
return parent::getTablesUsedInFrom(); |
820
|
|
|
} |
821
|
|
|
|
822
|
|
|
/** |
823
|
|
|
* @return string primary table name |
824
|
|
|
* @since 2.0.12 |
825
|
|
|
*/ |
826
|
399 |
|
protected function getPrimaryTableName() |
827
|
|
|
{ |
828
|
|
|
/* @var $modelClass ActiveRecord */ |
829
|
399 |
|
$modelClass = $this->modelClass; |
830
|
399 |
|
return $modelClass::tableName(); |
831
|
|
|
} |
832
|
|
|
} |
833
|
|
|
|
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..