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