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; |
11
|
|
|
use yii\base\Component; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Query represents a SELECT SQL statement in a way that is independent of DBMS. |
15
|
|
|
* |
16
|
|
|
* Query provides a set of methods to facilitate the specification of different clauses |
17
|
|
|
* in a SELECT statement. These methods can be chained together. |
18
|
|
|
* |
19
|
|
|
* By calling [[createCommand()]], we can get a [[Command]] instance which can be further |
20
|
|
|
* used to perform/execute the DB query against a database. |
21
|
|
|
* |
22
|
|
|
* For example, |
23
|
|
|
* |
24
|
|
|
* ```php |
25
|
|
|
* $query = new Query; |
26
|
|
|
* // compose the query |
27
|
|
|
* $query->select('id, name') |
28
|
|
|
* ->from('user') |
29
|
|
|
* ->limit(10); |
30
|
|
|
* // build and execute the query |
31
|
|
|
* $rows = $query->all(); |
32
|
|
|
* // alternatively, you can create DB command and execute it |
33
|
|
|
* $command = $query->createCommand(); |
34
|
|
|
* // $command->sql returns the actual SQL |
35
|
|
|
* $rows = $command->queryAll(); |
36
|
|
|
* ``` |
37
|
|
|
* |
38
|
|
|
* Query internally uses the [[QueryBuilder]] class to generate the SQL statement. |
39
|
|
|
* |
40
|
|
|
* A more detailed usage guide on how to work with Query can be found in the [guide article on Query Builder](guide:db-query-builder). |
41
|
|
|
* |
42
|
|
|
* @author Qiang Xue <[email protected]> |
43
|
|
|
* @author Carsten Brandt <[email protected]> |
44
|
|
|
* @since 2.0 |
45
|
|
|
*/ |
46
|
|
|
class Query extends Component implements QueryInterface |
47
|
|
|
{ |
48
|
|
|
use QueryTrait; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array the columns being selected. For example, `['id', 'name']`. |
52
|
|
|
* This is used to construct the SELECT clause in a SQL statement. If not set, it means selecting all columns. |
53
|
|
|
* @see select() |
54
|
|
|
*/ |
55
|
|
|
public $select; |
56
|
|
|
/** |
57
|
|
|
* @var string additional option that should be appended to the 'SELECT' keyword. For example, |
58
|
|
|
* in MySQL, the option 'SQL_CALC_FOUND_ROWS' can be used. |
59
|
|
|
*/ |
60
|
|
|
public $selectOption; |
61
|
|
|
/** |
62
|
|
|
* @var boolean whether to select distinct rows of data only. If this is set true, |
63
|
|
|
* the SELECT clause would be changed to SELECT DISTINCT. |
64
|
|
|
*/ |
65
|
|
|
public $distinct; |
66
|
|
|
/** |
67
|
|
|
* @var array the table(s) to be selected from. For example, `['user', 'post']`. |
68
|
|
|
* This is used to construct the FROM clause in a SQL statement. |
69
|
|
|
* @see from() |
70
|
|
|
*/ |
71
|
|
|
public $from; |
72
|
|
|
/** |
73
|
|
|
* @var array how to group the query results. For example, `['company', 'department']`. |
74
|
|
|
* This is used to construct the GROUP BY clause in a SQL statement. |
75
|
|
|
*/ |
76
|
|
|
public $groupBy; |
77
|
|
|
/** |
78
|
|
|
* @var array how to join with other tables. Each array element represents the specification |
79
|
|
|
* of one join which has the following structure: |
80
|
|
|
* |
81
|
|
|
* ```php |
82
|
|
|
* [$joinType, $tableName, $joinCondition] |
83
|
|
|
* ``` |
84
|
|
|
* |
85
|
|
|
* For example, |
86
|
|
|
* |
87
|
|
|
* ```php |
88
|
|
|
* [ |
89
|
|
|
* ['INNER JOIN', 'user', 'user.id = author_id'], |
90
|
|
|
* ['LEFT JOIN', 'team', 'team.id = team_id'], |
91
|
|
|
* ] |
92
|
|
|
* ``` |
93
|
|
|
*/ |
94
|
|
|
public $join; |
95
|
|
|
/** |
96
|
|
|
* @var string|array the condition to be applied in the GROUP BY clause. |
97
|
|
|
* It can be either a string or an array. Please refer to [[where()]] on how to specify the condition. |
98
|
|
|
*/ |
99
|
|
|
public $having; |
100
|
|
|
/** |
101
|
|
|
* @var array this is used to construct the UNION clause(s) in a SQL statement. |
102
|
|
|
* Each array element is an array of the following structure: |
103
|
|
|
* |
104
|
|
|
* - `query`: either a string or a [[Query]] object representing a query |
105
|
|
|
* - `all`: boolean, whether it should be `UNION ALL` or `UNION` |
106
|
|
|
*/ |
107
|
|
|
public $union; |
108
|
|
|
/** |
109
|
|
|
* @var array list of query parameter values indexed by parameter placeholders. |
110
|
|
|
* For example, `[':name' => 'Dan', ':age' => 31]`. |
111
|
|
|
*/ |
112
|
|
|
public $params = []; |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Creates a DB command that can be used to execute this query. |
117
|
|
|
* @param Connection $db the database connection used to generate the SQL statement. |
118
|
|
|
* If this parameter is not given, the `db` application component will be used. |
119
|
|
|
* @return Command the created DB command instance. |
120
|
|
|
*/ |
121
|
122 |
|
public function createCommand($db = null) |
122
|
|
|
{ |
123
|
122 |
|
if ($db === null) { |
124
|
7 |
|
$db = Yii::$app->getDb(); |
125
|
7 |
|
} |
126
|
122 |
|
list ($sql, $params) = $db->getQueryBuilder()->build($this); |
127
|
|
|
|
128
|
122 |
|
return $db->createCommand($sql, $params); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Prepares for building SQL. |
133
|
|
|
* This method is called by [[QueryBuilder]] when it starts to build SQL from a query object. |
134
|
|
|
* You may override this method to do some final preparation work when converting a query into a SQL statement. |
135
|
|
|
* @param QueryBuilder $builder |
136
|
|
|
* @return $this a prepared query instance which will be used by [[QueryBuilder]] to build the SQL |
137
|
|
|
*/ |
138
|
396 |
|
public function prepare($builder) |
|
|
|
|
139
|
|
|
{ |
140
|
396 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Starts a batch query. |
145
|
|
|
* |
146
|
|
|
* A batch query supports fetching data in batches, which can keep the memory usage under a limit. |
147
|
|
|
* This method will return a [[BatchQueryResult]] object which implements the [[\Iterator]] interface |
148
|
|
|
* and can be traversed to retrieve the data in batches. |
149
|
|
|
* |
150
|
|
|
* For example, |
151
|
|
|
* |
152
|
|
|
* ```php |
153
|
|
|
* $query = (new Query)->from('user'); |
154
|
|
|
* foreach ($query->batch() as $rows) { |
155
|
|
|
* // $rows is an array of 10 or fewer rows from user table |
156
|
|
|
* } |
157
|
|
|
* ``` |
158
|
|
|
* |
159
|
|
|
* @param integer $batchSize the number of records to be fetched in each batch. |
160
|
|
|
* @param Connection $db the database connection. If not set, the "db" application component will be used. |
161
|
|
|
* @return BatchQueryResult the batch query result. It implements the [[\Iterator]] interface |
162
|
|
|
* and can be traversed to retrieve the data in batches. |
163
|
|
|
*/ |
164
|
2 |
|
public function batch($batchSize = 100, $db = null) |
165
|
|
|
{ |
166
|
2 |
|
return Yii::createObject([ |
167
|
2 |
|
'class' => BatchQueryResult::className(), |
168
|
2 |
|
'query' => $this, |
169
|
2 |
|
'batchSize' => $batchSize, |
170
|
2 |
|
'db' => $db, |
171
|
2 |
|
'each' => false, |
172
|
2 |
|
]); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Starts a batch query and retrieves data row by row. |
177
|
|
|
* This method is similar to [[batch()]] except that in each iteration of the result, |
178
|
|
|
* only one row of data is returned. For example, |
179
|
|
|
* |
180
|
|
|
* ```php |
181
|
|
|
* $query = (new Query)->from('user'); |
182
|
|
|
* foreach ($query->each() as $row) { |
183
|
|
|
* } |
184
|
|
|
* ``` |
185
|
|
|
* |
186
|
|
|
* @param integer $batchSize the number of records to be fetched in each batch. |
187
|
|
|
* @param Connection $db the database connection. If not set, the "db" application component will be used. |
188
|
|
|
* @return BatchQueryResult the batch query result. It implements the [[\Iterator]] interface |
189
|
|
|
* and can be traversed to retrieve the data in batches. |
190
|
|
|
*/ |
191
|
1 |
|
public function each($batchSize = 100, $db = null) |
192
|
|
|
{ |
193
|
1 |
|
return Yii::createObject([ |
194
|
1 |
|
'class' => BatchQueryResult::className(), |
195
|
1 |
|
'query' => $this, |
196
|
1 |
|
'batchSize' => $batchSize, |
197
|
1 |
|
'db' => $db, |
198
|
1 |
|
'each' => true, |
199
|
1 |
|
]); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Executes the query and returns all results as an array. |
204
|
|
|
* @param Connection $db the database connection used to generate the SQL statement. |
205
|
|
|
* If this parameter is not given, the `db` application component will be used. |
206
|
|
|
* @return array the query results. If the query results in nothing, an empty array will be returned. |
207
|
|
|
*/ |
208
|
217 |
|
public function all($db = null) |
209
|
|
|
{ |
210
|
217 |
|
$rows = $this->createCommand($db)->queryAll(); |
211
|
217 |
|
return $this->populate($rows); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Converts the raw query results into the format as specified by this query. |
216
|
|
|
* This method is internally used to convert the data fetched from database |
217
|
|
|
* into the format as required by this query. |
218
|
|
|
* @param array $rows the raw query result from database |
219
|
|
|
* @return array the converted query result |
220
|
|
|
*/ |
221
|
74 |
|
public function populate($rows) |
222
|
|
|
{ |
223
|
74 |
|
if ($this->indexBy === null) { |
224
|
74 |
|
return $rows; |
225
|
|
|
} |
226
|
1 |
|
$result = []; |
227
|
1 |
|
foreach ($rows as $row) { |
228
|
1 |
|
if (is_string($this->indexBy)) { |
229
|
1 |
|
$key = $row[$this->indexBy]; |
230
|
1 |
|
} else { |
231
|
|
|
$key = call_user_func($this->indexBy, $row); |
232
|
|
|
} |
233
|
1 |
|
$result[$key] = $row; |
234
|
1 |
|
} |
235
|
1 |
|
return $result; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Executes the query and returns a single row of result. |
240
|
|
|
* @param Connection $db the database connection used to generate the SQL statement. |
241
|
|
|
* If this parameter is not given, the `db` application component will be used. |
242
|
|
|
* @return array|boolean the first row (in terms of an array) of the query result. False is returned if the query |
243
|
|
|
* results in nothing. |
244
|
|
|
*/ |
245
|
194 |
|
public function one($db = null) |
246
|
|
|
{ |
247
|
194 |
|
return $this->createCommand($db)->queryOne(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Returns the query result as a scalar value. |
252
|
|
|
* The value returned will be the first column in the first row of the query results. |
253
|
|
|
* @param Connection $db the database connection used to generate the SQL statement. |
254
|
|
|
* If this parameter is not given, the `db` application component will be used. |
255
|
|
|
* @return string|boolean the value of the first column in the first row of the query result. |
256
|
|
|
* False is returned if the query result is empty. |
257
|
|
|
*/ |
258
|
8 |
|
public function scalar($db = null) |
259
|
|
|
{ |
260
|
8 |
|
return $this->createCommand($db)->queryScalar(); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Executes the query and returns the first column of the result. |
265
|
|
|
* @param Connection $db the database connection used to generate the SQL statement. |
266
|
|
|
* If this parameter is not given, the `db` application component will be used. |
267
|
|
|
* @return array the first column of the query result. An empty array is returned if the query results in nothing. |
268
|
|
|
*/ |
269
|
17 |
|
public function column($db = null) |
270
|
|
|
{ |
271
|
17 |
|
if (!is_string($this->indexBy)) { |
272
|
17 |
|
return $this->createCommand($db)->queryColumn(); |
273
|
|
|
} |
274
|
3 |
|
if (is_array($this->select) && count($this->select) === 1) { |
275
|
3 |
|
$this->select[] = $this->indexBy; |
276
|
3 |
|
} |
277
|
3 |
|
$rows = $this->createCommand($db)->queryAll(); |
278
|
3 |
|
$results = []; |
279
|
3 |
|
foreach ($rows as $row) { |
280
|
3 |
|
if (array_key_exists($this->indexBy, $row)) { |
281
|
3 |
|
$results[$row[$this->indexBy]] = reset($row); |
282
|
3 |
|
} else { |
283
|
|
|
$results[] = reset($row); |
284
|
|
|
} |
285
|
3 |
|
} |
286
|
3 |
|
return $results; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Returns the number of records. |
291
|
|
|
* @param string $q the COUNT expression. Defaults to '*'. |
292
|
|
|
* Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression. |
293
|
|
|
* @param Connection $db the database connection used to generate the SQL statement. |
294
|
|
|
* If this parameter is not given (or null), the `db` application component will be used. |
295
|
|
|
* @return integer|string number of records. The result may be a string depending on the |
296
|
|
|
* underlying database engine and to support integer values higher than a 32bit PHP integer can handle. |
297
|
|
|
*/ |
298
|
69 |
|
public function count($q = '*', $db = null) |
299
|
|
|
{ |
300
|
69 |
|
return $this->queryScalar("COUNT($q)", $db); |
|
|
|
|
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Returns the sum of the specified column values. |
305
|
|
|
* @param string $q the column name or expression. |
306
|
|
|
* Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression. |
307
|
|
|
* @param Connection $db the database connection used to generate the SQL statement. |
308
|
|
|
* If this parameter is not given, the `db` application component will be used. |
309
|
|
|
* @return mixed the sum of the specified column values. |
310
|
|
|
*/ |
311
|
3 |
|
public function sum($q, $db = null) |
312
|
|
|
{ |
313
|
3 |
|
return $this->queryScalar("SUM($q)", $db); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Returns the average of the specified column values. |
318
|
|
|
* @param string $q the column name or expression. |
319
|
|
|
* Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression. |
320
|
|
|
* @param Connection $db the database connection used to generate the SQL statement. |
321
|
|
|
* If this parameter is not given, the `db` application component will be used. |
322
|
|
|
* @return mixed the average of the specified column values. |
323
|
|
|
*/ |
324
|
3 |
|
public function average($q, $db = null) |
325
|
|
|
{ |
326
|
3 |
|
return $this->queryScalar("AVG($q)", $db); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Returns the minimum of the specified column values. |
331
|
|
|
* @param string $q the column name or expression. |
332
|
|
|
* Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression. |
333
|
|
|
* @param Connection $db the database connection used to generate the SQL statement. |
334
|
|
|
* If this parameter is not given, the `db` application component will be used. |
335
|
|
|
* @return mixed the minimum of the specified column values. |
336
|
|
|
*/ |
337
|
3 |
|
public function min($q, $db = null) |
338
|
|
|
{ |
339
|
3 |
|
return $this->queryScalar("MIN($q)", $db); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Returns the maximum of the specified column values. |
344
|
|
|
* @param string $q the column name or expression. |
345
|
|
|
* Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression. |
346
|
|
|
* @param Connection $db the database connection used to generate the SQL statement. |
347
|
|
|
* If this parameter is not given, the `db` application component will be used. |
348
|
|
|
* @return mixed the maximum of the specified column values. |
349
|
|
|
*/ |
350
|
3 |
|
public function max($q, $db = null) |
351
|
|
|
{ |
352
|
3 |
|
return $this->queryScalar("MAX($q)", $db); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Returns a value indicating whether the query result contains any row of data. |
357
|
|
|
* @param Connection $db the database connection used to generate the SQL statement. |
358
|
|
|
* If this parameter is not given, the `db` application component will be used. |
359
|
|
|
* @return boolean whether the query result contains any row of data. |
360
|
|
|
*/ |
361
|
36 |
|
public function exists($db = null) |
362
|
|
|
{ |
363
|
36 |
|
$select = $this->select; |
364
|
36 |
|
$this->select = [new Expression('1')]; |
365
|
36 |
|
$command = $this->createCommand($db); |
366
|
36 |
|
$this->select = $select; |
367
|
36 |
|
return $command->queryScalar() !== false; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Queries a scalar value by setting [[select]] first. |
372
|
|
|
* Restores the value of select to make this query reusable. |
373
|
|
|
* @param string|Expression $selectExpression |
374
|
|
|
* @param Connection|null $db |
375
|
|
|
* @return bool|string |
376
|
|
|
*/ |
377
|
66 |
|
protected function queryScalar($selectExpression, $db) |
378
|
|
|
{ |
379
|
66 |
|
$select = $this->select; |
380
|
66 |
|
$limit = $this->limit; |
381
|
66 |
|
$offset = $this->offset; |
382
|
|
|
|
383
|
66 |
|
$this->select = [$selectExpression]; |
384
|
66 |
|
$this->limit = null; |
385
|
66 |
|
$this->offset = null; |
386
|
66 |
|
$command = $this->createCommand($db); |
387
|
|
|
|
388
|
66 |
|
$this->select = $select; |
389
|
66 |
|
$this->limit = $limit; |
390
|
66 |
|
$this->offset = $offset; |
391
|
|
|
|
392
|
66 |
|
if (empty($this->groupBy) && empty($this->having) && empty($this->union) && !$this->distinct) { |
393
|
65 |
|
return $command->queryScalar(); |
394
|
|
|
} else { |
395
|
7 |
|
return (new Query)->select([$selectExpression]) |
396
|
7 |
|
->from(['c' => $this]) |
397
|
7 |
|
->createCommand($command->db) |
398
|
7 |
|
->queryScalar(); |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Sets the SELECT part of the query. |
404
|
|
|
* @param string|array|Expression $columns the columns to be selected. |
405
|
|
|
* Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']). |
406
|
|
|
* Columns can be prefixed with table names (e.g. "user.id") and/or contain column aliases (e.g. "user.id AS user_id"). |
407
|
|
|
* The method will automatically quote the column names unless a column contains some parenthesis |
408
|
|
|
* (which means the column contains a DB expression). A DB expression may also be passed in form of |
409
|
|
|
* an [[Expression]] object. |
410
|
|
|
* |
411
|
|
|
* Note that if you are selecting an expression like `CONCAT(first_name, ' ', last_name)`, you should |
412
|
|
|
* use an array to specify the columns. Otherwise, the expression may be incorrectly split into several parts. |
413
|
|
|
* |
414
|
|
|
* When the columns are specified as an array, you may also use array keys as the column aliases (if a column |
415
|
|
|
* does not need alias, do not use a string key). |
416
|
|
|
* |
417
|
|
|
* Starting from version 2.0.1, you may also select sub-queries as columns by specifying each such column |
418
|
|
|
* as a `Query` instance representing the sub-query. |
419
|
|
|
* |
420
|
|
|
* @param string $option additional option that should be appended to the 'SELECT' keyword. For example, |
421
|
|
|
* in MySQL, the option 'SQL_CALC_FOUND_ROWS' can be used. |
422
|
|
|
* @return $this the query object itself |
423
|
|
|
*/ |
424
|
153 |
|
public function select($columns, $option = null) |
425
|
|
|
{ |
426
|
153 |
|
if ($columns instanceof Expression) { |
427
|
3 |
|
$columns = [$columns]; |
428
|
153 |
|
} elseif (!is_array($columns)) { |
429
|
68 |
|
$columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY); |
430
|
68 |
|
} |
431
|
153 |
|
$this->select = $columns; |
432
|
153 |
|
$this->selectOption = $option; |
433
|
153 |
|
return $this; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Add more columns to the SELECT part of the query. |
438
|
|
|
* |
439
|
|
|
* Note, that if [[select]] has not been specified before, you should include `*` explicitly |
440
|
|
|
* if you want to select all remaining columns too: |
441
|
|
|
* |
442
|
|
|
* ```php |
443
|
|
|
* $query->addSelect(["*", "CONCAT(first_name, ' ', last_name) AS full_name"])->one(); |
444
|
|
|
* ``` |
445
|
|
|
* |
446
|
|
|
* @param string|array|Expression $columns the columns to add to the select. See [[select()]] for more |
447
|
|
|
* details about the format of this parameter. |
448
|
|
|
* @return $this the query object itself |
449
|
|
|
* @see select() |
450
|
|
|
*/ |
451
|
9 |
|
public function addSelect($columns) |
452
|
|
|
{ |
453
|
9 |
|
if ($columns instanceof Expression) { |
454
|
3 |
|
$columns = [$columns]; |
455
|
9 |
|
} elseif (!is_array($columns)) { |
456
|
3 |
|
$columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY); |
457
|
3 |
|
} |
458
|
9 |
|
if ($this->select === null) { |
459
|
3 |
|
$this->select = $columns; |
460
|
3 |
|
} else { |
461
|
9 |
|
$this->select = array_merge($this->select, $columns); |
462
|
|
|
} |
463
|
9 |
|
return $this; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Sets the value indicating whether to SELECT DISTINCT or not. |
468
|
|
|
* @param boolean $value whether to SELECT DISTINCT or not. |
469
|
|
|
* @return $this the query object itself |
470
|
|
|
*/ |
471
|
6 |
|
public function distinct($value = true) |
472
|
|
|
{ |
473
|
6 |
|
$this->distinct = $value; |
474
|
6 |
|
return $this; |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Sets the FROM part of the query. |
479
|
|
|
* @param string|array $tables the table(s) to be selected from. This can be either a string (e.g. `'user'`) |
480
|
|
|
* or an array (e.g. `['user', 'profile']`) specifying one or several table names. |
481
|
|
|
* Table names can contain schema prefixes (e.g. `'public.user'`) and/or table aliases (e.g. `'user u'`). |
482
|
|
|
* The method will automatically quote the table names unless it contains some parenthesis |
483
|
|
|
* (which means the table is given as a sub-query or DB expression). |
484
|
|
|
* |
485
|
|
|
* When the tables are specified as an array, you may also use the array keys as the table aliases |
486
|
|
|
* (if a table does not need alias, do not use a string key). |
487
|
|
|
* |
488
|
|
|
* Use a Query object to represent a sub-query. In this case, the corresponding array key will be used |
489
|
|
|
* as the alias for the sub-query. |
490
|
|
|
* |
491
|
|
|
* Here are some examples: |
492
|
|
|
* |
493
|
|
|
* ```php |
494
|
|
|
* // SELECT * FROM `user` `u`, `profile`; |
495
|
|
|
* $query = (new \yii\db\Query)->from(['u' => 'user', 'profile']); |
496
|
|
|
* |
497
|
|
|
* // SELECT * FROM (SELECT * FROM `user` WHERE `active` = 1) `activeusers`; |
498
|
|
|
* $subquery = (new \yii\db\Query)->from('user')->where(['active' => true]) |
499
|
|
|
* $query = (new \yii\db\Query)->from(['activeusers' => $subquery]); |
500
|
|
|
* |
501
|
|
|
* // subquery can also be a string with plain SQL wrapped in parenthesis |
502
|
|
|
* // SELECT * FROM (SELECT * FROM `user` WHERE `active` = 1) `activeusers`; |
503
|
|
|
* $subquery = "(SELECT * FROM `user` WHERE `active` = 1)"; |
504
|
|
|
* $query = (new \yii\db\Query)->from(['activeusers' => $subquery]); |
505
|
|
|
* ``` |
506
|
|
|
* |
507
|
|
|
* @return $this the query object itself |
508
|
|
|
*/ |
509
|
179 |
|
public function from($tables) |
510
|
|
|
{ |
511
|
179 |
|
if (!is_array($tables)) { |
512
|
157 |
|
$tables = preg_split('/\s*,\s*/', trim($tables), -1, PREG_SPLIT_NO_EMPTY); |
513
|
157 |
|
} |
514
|
179 |
|
$this->from = $tables; |
515
|
179 |
|
$this->populateAliases((array) $tables); |
516
|
179 |
|
return $this; |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Sets the WHERE part of the query. |
521
|
|
|
* |
522
|
|
|
* The method requires a `$condition` parameter, and optionally a `$params` parameter |
523
|
|
|
* specifying the values to be bound to the query. |
524
|
|
|
* |
525
|
|
|
* The `$condition` parameter should be either a string (e.g. `'id=1'`) or an array. |
526
|
|
|
* |
527
|
|
|
* @inheritdoc |
528
|
|
|
* |
529
|
|
|
* @param string|array|Expression $condition the conditions that should be put in the WHERE part. |
530
|
|
|
* @param array $params the parameters (name => value) to be bound to the query. |
531
|
|
|
* @return $this the query object itself |
532
|
|
|
* @see andWhere() |
533
|
|
|
* @see orWhere() |
534
|
|
|
* @see QueryInterface::where() |
535
|
|
|
*/ |
536
|
398 |
|
public function where($condition, $params = []) |
537
|
|
|
{ |
538
|
398 |
|
$this->where = $condition; |
|
|
|
|
539
|
398 |
|
$this->addParams($params); |
540
|
398 |
|
return $this; |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
/** |
544
|
|
|
* Adds an additional WHERE condition to the existing one. |
545
|
|
|
* The new condition and the existing one will be joined using the 'AND' operator. |
546
|
|
|
* @param string|array|Expression $condition the new WHERE condition. Please refer to [[where()]] |
547
|
|
|
* on how to specify this parameter. |
548
|
|
|
* @param array $params the parameters (name => value) to be bound to the query. |
549
|
|
|
* @return $this the query object itself |
550
|
|
|
* @see where() |
551
|
|
|
* @see orWhere() |
552
|
|
|
*/ |
553
|
202 |
|
public function andWhere($condition, $params = []) |
554
|
|
|
{ |
555
|
202 |
|
if ($this->where === null) { |
556
|
176 |
|
$this->where = $condition; |
|
|
|
|
557
|
176 |
|
} else { |
558
|
65 |
|
$this->where = ['and', $this->where, $condition]; |
559
|
|
|
} |
560
|
202 |
|
$this->addParams($params); |
561
|
202 |
|
return $this; |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* Adds an additional WHERE condition to the existing one. |
566
|
|
|
* The new condition and the existing one will be joined using the 'OR' operator. |
567
|
|
|
* @param string|array|Expression $condition the new WHERE condition. Please refer to [[where()]] |
568
|
|
|
* on how to specify this parameter. |
569
|
|
|
* @param array $params the parameters (name => value) to be bound to the query. |
570
|
|
|
* @return $this the query object itself |
571
|
|
|
* @see where() |
572
|
|
|
* @see andWhere() |
573
|
|
|
*/ |
574
|
3 |
|
public function orWhere($condition, $params = []) |
575
|
|
|
{ |
576
|
3 |
|
if ($this->where === null) { |
577
|
|
|
$this->where = $condition; |
|
|
|
|
578
|
|
|
} else { |
579
|
3 |
|
$this->where = ['or', $this->where, $condition]; |
580
|
|
|
} |
581
|
3 |
|
$this->addParams($params); |
582
|
3 |
|
return $this; |
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
/** |
586
|
|
|
* Appends a JOIN part to the query. |
587
|
|
|
* The first parameter specifies what type of join it is. |
588
|
|
|
* @param string $type the type of join, such as INNER JOIN, LEFT JOIN. |
589
|
|
|
* @param string|array $table the table to be joined. |
590
|
|
|
* |
591
|
|
|
* Use a string to represent the name of the table to be joined. |
592
|
|
|
* The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). |
593
|
|
|
* The method will automatically quote the table name unless it contains some parenthesis |
594
|
|
|
* (which means the table is given as a sub-query or DB expression). |
595
|
|
|
* |
596
|
|
|
* Use an array to represent joining with a sub-query. The array must contain only one element. |
597
|
|
|
* The value must be a [[Query]] object representing the sub-query while the corresponding key |
598
|
|
|
* represents the alias for the sub-query. |
599
|
|
|
* |
600
|
|
|
* @param string|array $on the join condition that should appear in the ON part. |
601
|
|
|
* Please refer to [[where()]] on how to specify this parameter. |
602
|
|
|
* @param array $params the parameters (name => value) to be bound to the query. |
603
|
|
|
* @return $this the query object itself |
604
|
|
|
*/ |
605
|
30 |
|
public function join($type, $table, $on = '', $params = []) |
606
|
|
|
{ |
607
|
30 |
|
$this->join[] = [$type, $table, $on]; |
608
|
30 |
|
$this->populateAliases((array) $table); |
609
|
30 |
|
return $this->addParams($params); |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
/** |
613
|
|
|
* Appends an INNER JOIN part to the query. |
614
|
|
|
* @param string|array $table the table to be joined. |
615
|
|
|
* |
616
|
|
|
* Use a string to represent the name of the table to be joined. |
617
|
|
|
* The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). |
618
|
|
|
* The method will automatically quote the table name unless it contains some parenthesis |
619
|
|
|
* (which means the table is given as a sub-query or DB expression). |
620
|
|
|
* |
621
|
|
|
* Use an array to represent joining with a sub-query. The array must contain only one element. |
622
|
|
|
* The value must be a [[Query]] object representing the sub-query while the corresponding key |
623
|
|
|
* represents the alias for the sub-query. |
624
|
|
|
* |
625
|
|
|
* @param string|array $on the join condition that should appear in the ON part. |
626
|
|
|
* Please refer to [[where()]] on how to specify this parameter. |
627
|
|
|
* @param array $params the parameters (name => value) to be bound to the query. |
628
|
|
|
* @return $this the query object itself |
629
|
|
|
*/ |
630
|
3 |
|
public function innerJoin($table, $on = '', $params = []) |
631
|
|
|
{ |
632
|
3 |
|
$this->join[] = ['INNER JOIN', $table, $on]; |
633
|
3 |
|
$this->populateAliases((array) $table); |
634
|
3 |
|
return $this->addParams($params); |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
/** |
638
|
|
|
* Appends a LEFT OUTER JOIN part to the query. |
639
|
|
|
* @param string|array $table the table to be joined. |
640
|
|
|
* |
641
|
|
|
* Use a string to represent the name of the table to be joined. |
642
|
|
|
* The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). |
643
|
|
|
* The method will automatically quote the table name unless it contains some parenthesis |
644
|
|
|
* (which means the table is given as a sub-query or DB expression). |
645
|
|
|
* |
646
|
|
|
* Use an array to represent joining with a sub-query. The array must contain only one element. |
647
|
|
|
* The value must be a [[Query]] object representing the sub-query while the corresponding key |
648
|
|
|
* represents the alias for the sub-query. |
649
|
|
|
* |
650
|
|
|
* @param string|array $on the join condition that should appear in the ON part. |
651
|
|
|
* Please refer to [[where()]] on how to specify this parameter. |
652
|
|
|
* @param array $params the parameters (name => value) to be bound to the query |
653
|
|
|
* @return $this the query object itself |
654
|
|
|
*/ |
655
|
3 |
|
public function leftJoin($table, $on = '', $params = []) |
656
|
|
|
{ |
657
|
3 |
|
$this->join[] = ['LEFT JOIN', $table, $on]; |
658
|
3 |
|
$this->populateAliases((array) $table); |
659
|
3 |
|
return $this->addParams($params); |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
/** |
663
|
|
|
* Appends a RIGHT OUTER JOIN part to the query. |
664
|
|
|
* @param string|array $table the table to be joined. |
665
|
|
|
* |
666
|
|
|
* Use a string to represent the name of the table to be joined. |
667
|
|
|
* The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). |
668
|
|
|
* The method will automatically quote the table name unless it contains some parenthesis |
669
|
|
|
* (which means the table is given as a sub-query or DB expression). |
670
|
|
|
* |
671
|
|
|
* Use an array to represent joining with a sub-query. The array must contain only one element. |
672
|
|
|
* The value must be a [[Query]] object representing the sub-query while the corresponding key |
673
|
|
|
* represents the alias for the sub-query. |
674
|
|
|
* |
675
|
|
|
* @param string|array $on the join condition that should appear in the ON part. |
676
|
|
|
* Please refer to [[where()]] on how to specify this parameter. |
677
|
|
|
* @param array $params the parameters (name => value) to be bound to the query |
678
|
|
|
* @return $this the query object itself |
679
|
|
|
*/ |
680
|
3 |
|
public function rightJoin($table, $on = '', $params = []) |
681
|
|
|
{ |
682
|
3 |
|
$this->join[] = ['RIGHT JOIN', $table, $on]; |
683
|
3 |
|
$this->populateAliases((array) $table); |
684
|
3 |
|
return $this->addParams($params); |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* Sets the GROUP BY part of the query. |
689
|
|
|
* @param string|array|Expression $columns the columns to be grouped by. |
690
|
|
|
* Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']). |
691
|
|
|
* The method will automatically quote the column names unless a column contains some parenthesis |
692
|
|
|
* (which means the column contains a DB expression). |
693
|
|
|
* |
694
|
|
|
* Note that if your group-by is an expression containing commas, you should always use an array |
695
|
|
|
* to represent the group-by information. Otherwise, the method will not be able to correctly determine |
696
|
|
|
* the group-by columns. |
697
|
|
|
* |
698
|
|
|
* Since version 2.0.7, an [[Expression]] object can be passed to specify the GROUP BY part explicitly in plain SQL. |
699
|
|
|
* @return $this the query object itself |
700
|
|
|
* @see addGroupBy() |
701
|
|
|
*/ |
702
|
12 |
|
public function groupBy($columns) |
703
|
|
|
{ |
704
|
12 |
|
if ($columns instanceof Expression) { |
705
|
3 |
|
$columns = [$columns]; |
706
|
12 |
|
} elseif (!is_array($columns)) { |
707
|
12 |
|
$columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY); |
708
|
12 |
|
} |
709
|
12 |
|
$this->groupBy = $columns; |
710
|
12 |
|
return $this; |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
/** |
714
|
|
|
* Adds additional group-by columns to the existing ones. |
715
|
|
|
* @param string|array $columns additional columns to be grouped by. |
716
|
|
|
* Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']). |
717
|
|
|
* The method will automatically quote the column names unless a column contains some parenthesis |
718
|
|
|
* (which means the column contains a DB expression). |
719
|
|
|
* |
720
|
|
|
* Note that if your group-by is an expression containing commas, you should always use an array |
721
|
|
|
* to represent the group-by information. Otherwise, the method will not be able to correctly determine |
722
|
|
|
* the group-by columns. |
723
|
|
|
* |
724
|
|
|
* Since version 2.0.7, an [[Expression]] object can be passed to specify the GROUP BY part explicitly in plain SQL. |
725
|
|
|
* @return $this the query object itself |
726
|
|
|
* @see groupBy() |
727
|
|
|
*/ |
728
|
3 |
|
public function addGroupBy($columns) |
729
|
|
|
{ |
730
|
3 |
|
if ($columns instanceof Expression) { |
731
|
|
|
$columns = [$columns]; |
732
|
3 |
|
} elseif (!is_array($columns)) { |
733
|
3 |
|
$columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY); |
734
|
3 |
|
} |
735
|
3 |
|
if ($this->groupBy === null) { |
736
|
|
|
$this->groupBy = $columns; |
737
|
|
|
} else { |
738
|
3 |
|
$this->groupBy = array_merge($this->groupBy, $columns); |
739
|
|
|
} |
740
|
3 |
|
return $this; |
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
/** |
744
|
|
|
* Sets the HAVING part of the query. |
745
|
|
|
* @param string|array|Expression $condition the conditions to be put after HAVING. |
746
|
|
|
* Please refer to [[where()]] on how to specify this parameter. |
747
|
|
|
* @param array $params the parameters (name => value) to be bound to the query. |
748
|
|
|
* @return $this the query object itself |
749
|
|
|
* @see andHaving() |
750
|
|
|
* @see orHaving() |
751
|
|
|
*/ |
752
|
4 |
|
public function having($condition, $params = []) |
753
|
|
|
{ |
754
|
4 |
|
$this->having = $condition; |
|
|
|
|
755
|
4 |
|
$this->addParams($params); |
756
|
4 |
|
return $this; |
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
/** |
760
|
|
|
* Adds an additional HAVING condition to the existing one. |
761
|
|
|
* The new condition and the existing one will be joined using the 'AND' operator. |
762
|
|
|
* @param string|array|Expression $condition the new HAVING condition. Please refer to [[where()]] |
763
|
|
|
* on how to specify this parameter. |
764
|
|
|
* @param array $params the parameters (name => value) to be bound to the query. |
765
|
|
|
* @return $this the query object itself |
766
|
|
|
* @see having() |
767
|
|
|
* @see orHaving() |
768
|
|
|
*/ |
769
|
3 |
|
public function andHaving($condition, $params = []) |
770
|
|
|
{ |
771
|
3 |
|
if ($this->having === null) { |
772
|
|
|
$this->having = $condition; |
|
|
|
|
773
|
|
|
} else { |
774
|
3 |
|
$this->having = ['and', $this->having, $condition]; |
775
|
|
|
} |
776
|
3 |
|
$this->addParams($params); |
777
|
3 |
|
return $this; |
778
|
|
|
} |
779
|
|
|
|
780
|
|
|
/** |
781
|
|
|
* Adds an additional HAVING condition to the existing one. |
782
|
|
|
* The new condition and the existing one will be joined using the 'OR' operator. |
783
|
|
|
* @param string|array|Expression $condition the new HAVING condition. Please refer to [[where()]] |
784
|
|
|
* on how to specify this parameter. |
785
|
|
|
* @param array $params the parameters (name => value) to be bound to the query. |
786
|
|
|
* @return $this the query object itself |
787
|
|
|
* @see having() |
788
|
|
|
* @see andHaving() |
789
|
|
|
*/ |
790
|
3 |
|
public function orHaving($condition, $params = []) |
791
|
|
|
{ |
792
|
3 |
|
if ($this->having === null) { |
793
|
|
|
$this->having = $condition; |
|
|
|
|
794
|
|
|
} else { |
795
|
3 |
|
$this->having = ['or', $this->having, $condition]; |
796
|
|
|
} |
797
|
3 |
|
$this->addParams($params); |
798
|
3 |
|
return $this; |
799
|
|
|
} |
800
|
|
|
|
801
|
|
|
/** |
802
|
|
|
* Appends a SQL statement using UNION operator. |
803
|
|
|
* @param string|Query $sql the SQL statement to be appended using UNION |
804
|
|
|
* @param boolean $all TRUE if using UNION ALL and FALSE if using UNION |
805
|
|
|
* @return $this the query object itself |
806
|
|
|
*/ |
807
|
10 |
|
public function union($sql, $all = false) |
808
|
|
|
{ |
809
|
10 |
|
$this->union[] = ['query' => $sql, 'all' => $all]; |
810
|
10 |
|
return $this; |
811
|
|
|
} |
812
|
|
|
|
813
|
|
|
/** |
814
|
|
|
* Sets the parameters to be bound to the query. |
815
|
|
|
* @param array $params list of query parameter values indexed by parameter placeholders. |
816
|
|
|
* For example, `[':name' => 'Dan', ':age' => 31]`. |
817
|
|
|
* @return $this the query object itself |
818
|
|
|
* @see addParams() |
819
|
|
|
*/ |
820
|
6 |
|
public function params($params) |
821
|
|
|
{ |
822
|
6 |
|
$this->params = $params; |
823
|
6 |
|
return $this; |
824
|
|
|
} |
825
|
|
|
|
826
|
|
|
/** |
827
|
|
|
* Adds additional parameters to be bound to the query. |
828
|
|
|
* @param array $params list of query parameter values indexed by parameter placeholders. |
829
|
|
|
* For example, `[':name' => 'Dan', ':age' => 31]`. |
830
|
|
|
* @return $this the query object itself |
831
|
|
|
* @see params() |
832
|
|
|
*/ |
833
|
539 |
|
public function addParams($params) |
834
|
|
|
{ |
835
|
539 |
|
if (!empty($params)) { |
836
|
29 |
|
if (empty($this->params)) { |
837
|
29 |
|
$this->params = $params; |
838
|
29 |
|
} else { |
839
|
6 |
|
foreach ($params as $name => $value) { |
840
|
6 |
|
if (is_int($name)) { |
841
|
|
|
$this->params[] = $value; |
842
|
|
|
} else { |
843
|
6 |
|
$this->params[$name] = $value; |
844
|
|
|
} |
845
|
6 |
|
} |
846
|
|
|
} |
847
|
29 |
|
} |
848
|
539 |
|
return $this; |
849
|
|
|
} |
850
|
|
|
|
851
|
|
|
/** |
852
|
|
|
* @var array aliases defined for tables used in this query. `tablename => alias`. |
853
|
|
|
*/ |
854
|
|
|
private $_aliases = []; |
855
|
|
|
|
856
|
|
|
/** |
857
|
|
|
* Populate table aliases used in this query. |
858
|
|
|
* |
859
|
|
|
* This function is used by [[from()]] and [[join()]] to make information about |
860
|
|
|
* tables aliases available for [[getAlias()]]. |
861
|
|
|
* |
862
|
|
|
* @param array $tables array of tables in the format allowed for [[from()]]: |
863
|
|
|
* |
864
|
|
|
* - numeric key, string value - table without alias, or table with alias specified after the name, separated by white space. |
865
|
|
|
* - string key, string value - table with alias. |
866
|
|
|
* - string key, object value - subquery with alias. This alias will not be populated. |
867
|
|
|
* @see from() |
868
|
|
|
* @see join() |
869
|
|
|
* @see getAlias() |
870
|
|
|
* @since 2.0.7 |
871
|
|
|
*/ |
872
|
197 |
|
protected function populateAliases($tables) |
873
|
|
|
{ |
874
|
197 |
|
foreach ($tables as $alias => $tableName) { |
875
|
197 |
|
if (is_object($tableName)) { |
876
|
10 |
|
continue; |
877
|
|
|
} |
878
|
194 |
|
if (is_string($alias)) { |
879
|
57 |
|
$this->_aliases[$tableName] = $alias; |
880
|
194 |
|
} elseif (preg_match('/^(?:\\{\\{(\w+)\\}\\}|(.*?))(?:\s+AS\s+|\s+)(\\{\\{\w+\\}\\}|\w+)$/i', $tableName, $matches)) { |
881
|
33 |
|
$this->_aliases[$matches[1] ?: $matches[2]] = $matches[3]; |
882
|
33 |
|
} |
883
|
197 |
|
} |
884
|
197 |
|
} |
885
|
|
|
|
886
|
|
|
/** |
887
|
|
|
* Returns the alias of a table in this query. |
888
|
|
|
* |
889
|
|
|
* Aliases are available when a table alias has been specified by calling either [[from()]], |
890
|
|
|
* [[join()]] or other join methods. If no alias has been defined, the original table name is |
891
|
|
|
* returned without modification. |
892
|
|
|
* |
893
|
|
|
* Note, that if a table is used multiple times in the query (e.g. self-join situations) |
894
|
|
|
* relying on this function may not give the expected result. |
895
|
|
|
* |
896
|
|
|
* Usage example: |
897
|
|
|
* |
898
|
|
|
* ```php |
899
|
|
|
* $query = (new Query)->from(['u' => 'user']); |
900
|
|
|
* |
901
|
|
|
* echo $query->getAlias('user'); // "u" |
902
|
|
|
* $query->andWhere($query->applyAlias('user', 'name') => 'cebe'); |
903
|
|
|
* // SELECT * FROM user u WHERE u.name = 'cebe'; |
904
|
|
|
* ``` |
905
|
|
|
* |
906
|
|
|
* Note also, that using this method might not work in all cases because the alias may change |
907
|
|
|
* later when the query is modified. An alternative Syntax for resolving table aliases is |
908
|
|
|
* described in the [Guide about alias handling](db-querybuilder#aliases) and can be used as follows: |
909
|
|
|
* |
910
|
|
|
* ```php |
911
|
|
|
* // {{@user}} will resolve to the table alias |
912
|
|
|
* $query->where('{{@user}}.id = post.author_id') |
913
|
|
|
* ``` |
914
|
|
|
* |
915
|
|
|
* @param string $table the table name. |
916
|
|
|
* @return string the table alias. |
917
|
|
|
* @see applyAlias() for disambiguating columns. |
918
|
|
|
* @since 2.0.7 |
919
|
|
|
*/ |
920
|
24 |
|
public function getAlias($table) |
921
|
|
|
{ |
922
|
24 |
|
if (isset($this->_aliases[$table])) { |
923
|
24 |
|
return $this->_aliases[$table]; |
924
|
|
|
} |
925
|
21 |
|
return $table; |
926
|
|
|
} |
927
|
|
|
|
928
|
|
|
/** |
929
|
|
|
* Disambiguate a column name by looking up the table alias. |
930
|
|
|
* |
931
|
|
|
* This method will look up the alias for a table using [[getAlias()]] and |
932
|
|
|
* return the disambiguated column name. e.g. for `applyAlias('user', 'name')` |
933
|
|
|
* it will return `u.name` if the table alias was `u`. |
934
|
|
|
* |
935
|
|
|
* It can be used to disambiguate column names in situations |
936
|
|
|
* where the table alias of the table is unknown, e.g. the query has been |
937
|
|
|
* created in a different place and should now be extended by an additional condition. |
938
|
|
|
* |
939
|
|
|
* Usage example: |
940
|
|
|
* |
941
|
|
|
* ```php |
942
|
|
|
* // a function that adjusts a query by applying an additional check |
943
|
|
|
* function addPermissionCheck($query) |
944
|
|
|
* { |
945
|
|
|
* $query->andWhere([$query->applyAlias('user', 'isAdmin') => 1]); |
946
|
|
|
* } |
947
|
|
|
* |
948
|
|
|
* $query = (new Query)->from(['u' => 'user']); |
949
|
|
|
* addPermissionCheck($query); |
950
|
|
|
* // SELECT * FROM user u WHERE u.isAdmin = 1; |
951
|
|
|
* ``` |
952
|
|
|
* |
953
|
|
|
* @param string $table the table name. |
954
|
|
|
* @param string $column the column name. |
955
|
|
|
* @return string the table alias. |
956
|
|
|
* @see applyAlias() for disambiguating columns. |
957
|
|
|
* @since 2.0.7 |
958
|
|
|
*/ |
959
|
3 |
|
public function applyAlias($table, $column) |
960
|
|
|
{ |
961
|
3 |
|
return $this->getAlias($table) . '.' . $column; |
962
|
|
|
} |
963
|
|
|
|
964
|
|
|
/** |
965
|
|
|
* Creates a new Query object and copies its property values from an existing one. |
966
|
|
|
* The properties being copies are the ones to be used by query builders. |
967
|
|
|
* @param Query $from the source query object |
968
|
|
|
* @return Query the new Query object |
969
|
|
|
*/ |
970
|
226 |
|
public static function create($from) |
971
|
|
|
{ |
972
|
226 |
|
return new self([ |
973
|
226 |
|
'where' => $from->where, |
974
|
226 |
|
'limit' => $from->limit, |
975
|
226 |
|
'offset' => $from->offset, |
976
|
226 |
|
'orderBy' => $from->orderBy, |
977
|
226 |
|
'indexBy' => $from->indexBy, |
978
|
226 |
|
'select' => $from->select, |
979
|
226 |
|
'selectOption' => $from->selectOption, |
980
|
226 |
|
'distinct' => $from->distinct, |
981
|
226 |
|
'from' => $from->from, |
982
|
226 |
|
'groupBy' => $from->groupBy, |
983
|
226 |
|
'join' => $from->join, |
984
|
226 |
|
'having' => $from->having, |
985
|
226 |
|
'union' => $from->union, |
986
|
226 |
|
'params' => $from->params, |
987
|
226 |
|
]); |
988
|
226 |
|
} |
989
|
|
|
} |
990
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.