Complex classes like Query often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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 = []) |
|
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 = []) |
|
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 = []) |
|
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) |
|
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) |
|
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 = []) |
|
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 = []) |
|
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 = []) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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.