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 |
||
49 | class Query extends Component implements QueryInterface |
||
50 | { |
||
51 | use QueryTrait; |
||
52 | |||
53 | /** |
||
54 | * @var array the columns being selected. For example, `['id', 'name']`. |
||
55 | * This is used to construct the SELECT clause in a SQL statement. If not set, it means selecting all columns. |
||
56 | * @see select() |
||
57 | */ |
||
58 | public $select; |
||
59 | /** |
||
60 | * @var string additional option that should be appended to the 'SELECT' keyword. For example, |
||
61 | * in MySQL, the option 'SQL_CALC_FOUND_ROWS' can be used. |
||
62 | */ |
||
63 | public $selectOption; |
||
64 | /** |
||
65 | * @var bool whether to select distinct rows of data only. If this is set true, |
||
66 | * the SELECT clause would be changed to SELECT DISTINCT. |
||
67 | */ |
||
68 | public $distinct; |
||
69 | /** |
||
70 | * @var array the table(s) to be selected from. For example, `['user', 'post']`. |
||
71 | * This is used to construct the FROM clause in a SQL statement. |
||
72 | * @see from() |
||
73 | */ |
||
74 | public $from; |
||
75 | /** |
||
76 | * @var array how to group the query results. For example, `['company', 'department']`. |
||
77 | * This is used to construct the GROUP BY clause in a SQL statement. |
||
78 | */ |
||
79 | public $groupBy; |
||
80 | /** |
||
81 | * @var array how to join with other tables. Each array element represents the specification |
||
82 | * of one join which has the following structure: |
||
83 | * |
||
84 | * ```php |
||
85 | * [$joinType, $tableName, $joinCondition] |
||
86 | * ``` |
||
87 | * |
||
88 | * For example, |
||
89 | * |
||
90 | * ```php |
||
91 | * [ |
||
92 | * ['INNER JOIN', 'user', 'user.id = author_id'], |
||
93 | * ['LEFT JOIN', 'team', 'team.id = team_id'], |
||
94 | * ] |
||
95 | * ``` |
||
96 | */ |
||
97 | public $join; |
||
98 | /** |
||
99 | * @var string|array|Expression the condition to be applied in the GROUP BY clause. |
||
100 | * It can be either a string or an array. Please refer to [[where()]] on how to specify the condition. |
||
101 | */ |
||
102 | public $having; |
||
103 | /** |
||
104 | * @var array this is used to construct the UNION clause(s) in a SQL statement. |
||
105 | * Each array element is an array of the following structure: |
||
106 | * |
||
107 | * - `query`: either a string or a [[Query]] object representing a query |
||
108 | * - `all`: boolean, whether it should be `UNION ALL` or `UNION` |
||
109 | */ |
||
110 | public $union; |
||
111 | /** |
||
112 | * @var array list of query parameter values indexed by parameter placeholders. |
||
113 | * For example, `[':name' => 'Dan', ':age' => 31]`. |
||
114 | */ |
||
115 | public $params = []; |
||
116 | |||
117 | |||
118 | /** |
||
119 | * Creates a DB command that can be used to execute this query. |
||
120 | * @param Connection $db the database connection used to generate the SQL statement. |
||
121 | * If this parameter is not given, the `db` application component will be used. |
||
122 | * @return Command the created DB command instance. |
||
123 | */ |
||
124 | 333 | public function createCommand($db = null) |
|
133 | |||
134 | /** |
||
135 | * Prepares for building SQL. |
||
136 | * This method is called by [[QueryBuilder]] when it starts to build SQL from a query object. |
||
137 | * You may override this method to do some final preparation work when converting a query into a SQL statement. |
||
138 | * @param QueryBuilder $builder |
||
139 | * @return $this a prepared query instance which will be used by [[QueryBuilder]] to build the SQL |
||
140 | */ |
||
141 | 655 | public function prepare($builder) |
|
145 | |||
146 | /** |
||
147 | * Starts a batch query. |
||
148 | * |
||
149 | * A batch query supports fetching data in batches, which can keep the memory usage under a limit. |
||
150 | * This method will return a [[BatchQueryResult]] object which implements the [[\Iterator]] interface |
||
151 | * and can be traversed to retrieve the data in batches. |
||
152 | * |
||
153 | * For example, |
||
154 | * |
||
155 | * ```php |
||
156 | * $query = (new Query)->from('user'); |
||
157 | * foreach ($query->batch() as $rows) { |
||
158 | * // $rows is an array of 100 or fewer rows from user table |
||
159 | * } |
||
160 | * ``` |
||
161 | * |
||
162 | * @param int $batchSize the number of records to be fetched in each batch. |
||
163 | * @param Connection $db the database connection. If not set, the "db" application component will be used. |
||
164 | * @return BatchQueryResult the batch query result. It implements the [[\Iterator]] interface |
||
165 | * and can be traversed to retrieve the data in batches. |
||
166 | */ |
||
167 | 6 | public function batch($batchSize = 100, $db = null) |
|
177 | |||
178 | /** |
||
179 | * Starts a batch query and retrieves data row by row. |
||
180 | * |
||
181 | * This method is similar to [[batch()]] except that in each iteration of the result, |
||
182 | * only one row of data is returned. For example, |
||
183 | * |
||
184 | * ```php |
||
185 | * $query = (new Query)->from('user'); |
||
186 | * foreach ($query->each() as $row) { |
||
187 | * } |
||
188 | * ``` |
||
189 | * |
||
190 | * @param int $batchSize the number of records to be fetched in each batch. |
||
191 | * @param Connection $db the database connection. If not set, the "db" application component will be used. |
||
192 | * @return BatchQueryResult the batch query result. It implements the [[\Iterator]] interface |
||
193 | * and can be traversed to retrieve the data in batches. |
||
194 | */ |
||
195 | 3 | public function each($batchSize = 100, $db = null) |
|
205 | |||
206 | /** |
||
207 | * Executes the query and returns all results as an array. |
||
208 | * @param Connection $db the database connection used to generate the SQL statement. |
||
209 | * If this parameter is not given, the `db` application component will be used. |
||
210 | * @return array the query results. If the query results in nothing, an empty array will be returned. |
||
211 | */ |
||
212 | 409 | public function all($db = null) |
|
220 | |||
221 | /** |
||
222 | * Converts the raw query results into the format as specified by this query. |
||
223 | * This method is internally used to convert the data fetched from database |
||
224 | * into the format as required by this query. |
||
225 | * @param array $rows the raw query result from database |
||
226 | * @return array the converted query result |
||
227 | */ |
||
228 | 223 | public function populate($rows) |
|
245 | |||
246 | /** |
||
247 | * Executes the query and returns a single row of result. |
||
248 | * @param Connection $db the database connection used to generate the SQL statement. |
||
249 | * If this parameter is not given, the `db` application component will be used. |
||
250 | * @return array|bool the first row (in terms of an array) of the query result. False is returned if the query |
||
251 | * results in nothing. |
||
252 | */ |
||
253 | 395 | public function one($db = null) |
|
261 | |||
262 | /** |
||
263 | * Returns the query result as a scalar value. |
||
264 | * The value returned will be the first column in the first row of the query results. |
||
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 string|null|false the value of the first column in the first row of the query result. |
||
268 | * False is returned if the query result is empty. |
||
269 | */ |
||
270 | 24 | public function scalar($db = null) |
|
278 | |||
279 | /** |
||
280 | * Executes the query and returns the first column of the result. |
||
281 | * @param Connection $db the database connection used to generate the SQL statement. |
||
282 | * If this parameter is not given, the `db` application component will be used. |
||
283 | * @return array the first column of the query result. An empty array is returned if the query results in nothing. |
||
284 | */ |
||
285 | 67 | public function column($db = null) |
|
316 | |||
317 | /** |
||
318 | * Returns the number of records. |
||
319 | * @param string $q the COUNT expression. Defaults to '*'. |
||
320 | * Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression. |
||
321 | * @param Connection $db the database connection used to generate the SQL statement. |
||
322 | * If this parameter is not given (or null), the `db` application component will be used. |
||
323 | * @return int|string number of records. The result may be a string depending on the |
||
324 | * underlying database engine and to support integer values higher than a 32bit PHP integer can handle. |
||
325 | */ |
||
326 | 87 | public function count($q = '*', $db = null) |
|
334 | |||
335 | /** |
||
336 | * Returns the sum of the specified column values. |
||
337 | * @param string $q the column name or expression. |
||
338 | * Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression. |
||
339 | * @param Connection $db the database connection used to generate the SQL statement. |
||
340 | * If this parameter is not given, the `db` application component will be used. |
||
341 | * @return mixed the sum of the specified column values. |
||
342 | */ |
||
343 | 9 | public function sum($q, $db = null) |
|
351 | |||
352 | /** |
||
353 | * Returns the average of the specified column values. |
||
354 | * @param string $q the column name or expression. |
||
355 | * Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression. |
||
356 | * @param Connection $db the database connection used to generate the SQL statement. |
||
357 | * If this parameter is not given, the `db` application component will be used. |
||
358 | * @return mixed the average of the specified column values. |
||
359 | */ |
||
360 | 9 | public function average($q, $db = null) |
|
368 | |||
369 | /** |
||
370 | * Returns the minimum of the specified column values. |
||
371 | * @param string $q the column name or expression. |
||
372 | * Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression. |
||
373 | * @param Connection $db the database connection used to generate the SQL statement. |
||
374 | * If this parameter is not given, the `db` application component will be used. |
||
375 | * @return mixed the minimum of the specified column values. |
||
376 | */ |
||
377 | 9 | public function min($q, $db = null) |
|
381 | |||
382 | /** |
||
383 | * Returns the maximum of the specified column values. |
||
384 | * @param string $q the column name or expression. |
||
385 | * Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression. |
||
386 | * @param Connection $db the database connection used to generate the SQL statement. |
||
387 | * If this parameter is not given, the `db` application component will be used. |
||
388 | * @return mixed the maximum of the specified column values. |
||
389 | */ |
||
390 | 9 | public function max($q, $db = null) |
|
394 | |||
395 | /** |
||
396 | * Returns a value indicating whether the query result contains any row of data. |
||
397 | * @param Connection $db the database connection used to generate the SQL statement. |
||
398 | * If this parameter is not given, the `db` application component will be used. |
||
399 | * @return bool whether the query result contains any row of data. |
||
400 | */ |
||
401 | 67 | public function exists($db = null) |
|
412 | |||
413 | /** |
||
414 | * Queries a scalar value by setting [[select]] first. |
||
415 | * Restores the value of select to make this query reusable. |
||
416 | * @param string|Expression $selectExpression |
||
417 | * @param Connection|null $db |
||
418 | * @return bool|string |
||
419 | */ |
||
420 | 87 | protected function queryScalar($selectExpression, $db) |
|
457 | |||
458 | /** |
||
459 | * Returns table names used in [[from]] indexed by aliases. |
||
460 | * Both aliases and names are enclosed into {{ and }}. |
||
461 | * @return string[] table names indexed by aliases |
||
462 | * @throws \yii\base\InvalidConfigException |
||
463 | * @since 2.0.12 |
||
464 | */ |
||
465 | 140 | public function getTablesUsedInFrom() |
|
466 | { |
||
467 | 140 | if (empty($this->from)) { |
|
468 | 3 | return []; |
|
469 | } |
||
470 | |||
471 | 137 | if (is_array($this->from)) { |
|
472 | 101 | $tableNames = $this->from; |
|
473 | 36 | } elseif (is_string($this->from)) { |
|
474 | 24 | $tableNames = preg_split('/\s*,\s*/', trim($this->from), -1, PREG_SPLIT_NO_EMPTY); |
|
475 | 12 | } elseif ($this->from instanceof Expression) { |
|
476 | 6 | $tableNames[] = $this->from; |
|
477 | } else { |
||
478 | 6 | throw new InvalidConfigException(gettype($this->from) . ' in $from is not supported.'); |
|
479 | } |
||
480 | |||
481 | // Clean up table names and aliases |
||
482 | 131 | $cleanedUpTableNames = []; |
|
483 | 131 | foreach ($tableNames as $alias => $tableName) { |
|
484 | 131 | $tableNameString = $tableName; |
|
485 | 131 | if ($tableName instanceof Expression) { |
|
486 | 12 | $tableNameString = $tableName->expression; |
|
487 | } |
||
488 | |||
489 | 131 | if (!is_string($alias) && is_string($tableNameString)) { |
|
490 | $pattern = <<<PATTERN |
||
491 | 119 | ~ |
|
492 | ^ |
||
493 | \s* |
||
494 | ( |
||
495 | (?:['"`\[]|{{) |
||
496 | .*? |
||
497 | (?:['"`\]]|}}) |
||
498 | | |
||
499 | \(.*?\) |
||
500 | | |
||
501 | .*? |
||
502 | ) |
||
503 | (?: |
||
504 | (?: |
||
505 | \s+ |
||
506 | (?:as)? |
||
507 | \s* |
||
508 | ) |
||
509 | ( |
||
510 | (?:['"`\[]|{{) |
||
511 | .*? |
||
512 | (?:['"`\]]|}}) |
||
513 | | |
||
514 | .*? |
||
515 | ) |
||
516 | )? |
||
517 | \s* |
||
518 | $ |
||
519 | ~iux |
||
520 | PATTERN; |
||
521 | 119 | if (preg_match($pattern, $tableNameString, $matches)) { |
|
522 | 119 | if (isset($matches[1])) { |
|
523 | 119 | if (isset($matches[2])) { |
|
524 | 24 | list(, $tableNameString, $alias) = $matches; |
|
525 | } else { |
||
526 | 107 | $tableNameString = $alias = $matches[1]; |
|
527 | } |
||
528 | } |
||
529 | } |
||
530 | } |
||
531 | |||
532 | 131 | if ($tableName instanceof Expression) { |
|
533 | 12 | $cleanedUpTableNames[$this->ensureNameQuoted($alias)] = $tableNameString; |
|
534 | 119 | } elseif ($tableName instanceof self) { |
|
535 | 6 | $cleanedUpTableNames[$this->ensureNameQuoted($alias)] = $tableName; |
|
536 | } else { |
||
537 | 131 | $cleanedUpTableNames[$this->ensureNameQuoted($alias)] = $this->ensureNameQuoted($tableNameString); |
|
538 | } |
||
539 | } |
||
540 | |||
541 | 131 | return $cleanedUpTableNames; |
|
542 | } |
||
543 | |||
544 | /** |
||
545 | * Ensures name is wrapped with {{ and }} |
||
546 | * @param string $name |
||
547 | * @return string |
||
548 | */ |
||
549 | 131 | private function ensureNameQuoted($name) |
|
550 | { |
||
551 | 131 | $name = str_replace(["'", '"', '`', '[', ']'], '', $name); |
|
552 | 131 | if ($name && !preg_match('/^{{.*}}$/', $name)) { |
|
553 | 119 | return '{{' . $name . '}}'; |
|
554 | } |
||
555 | |||
556 | 30 | return $name; |
|
557 | } |
||
558 | |||
559 | /** |
||
560 | * Sets the SELECT part of the query. |
||
561 | * @param string|array|Expression $columns the columns to be selected. |
||
562 | * Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']). |
||
563 | * Columns can be prefixed with table names (e.g. "user.id") and/or contain column aliases (e.g. "user.id AS user_id"). |
||
564 | * The method will automatically quote the column names unless a column contains some parenthesis |
||
565 | * (which means the column contains a DB expression). A DB expression may also be passed in form of |
||
566 | * an [[Expression]] object. |
||
567 | * |
||
568 | * Note that if you are selecting an expression like `CONCAT(first_name, ' ', last_name)`, you should |
||
569 | * use an array to specify the columns. Otherwise, the expression may be incorrectly split into several parts. |
||
570 | * |
||
571 | * When the columns are specified as an array, you may also use array keys as the column aliases (if a column |
||
572 | * does not need alias, do not use a string key). |
||
573 | * |
||
574 | * Starting from version 2.0.1, you may also select sub-queries as columns by specifying each such column |
||
575 | * as a `Query` instance representing the sub-query. |
||
576 | * |
||
577 | * @param string $option additional option that should be appended to the 'SELECT' keyword. For example, |
||
578 | * in MySQL, the option 'SQL_CALC_FOUND_ROWS' can be used. |
||
579 | * @return $this the query object itself |
||
580 | */ |
||
581 | 366 | public function select($columns, $option = null) |
|
582 | { |
||
583 | 366 | if ($columns instanceof Expression) { |
|
584 | 3 | $columns = [$columns]; |
|
585 | 363 | } elseif (!is_array($columns)) { |
|
586 | 104 | $columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY); |
|
587 | } |
||
588 | 366 | $this->select = $columns; |
|
589 | 366 | $this->selectOption = $option; |
|
590 | 366 | return $this; |
|
591 | } |
||
592 | |||
593 | /** |
||
594 | * Add more columns to the SELECT part of the query. |
||
595 | * |
||
596 | * Note, that if [[select]] has not been specified before, you should include `*` explicitly |
||
597 | * if you want to select all remaining columns too: |
||
598 | * |
||
599 | * ```php |
||
600 | * $query->addSelect(["*", "CONCAT(first_name, ' ', last_name) AS full_name"])->one(); |
||
601 | * ``` |
||
602 | * |
||
603 | * @param string|array|Expression $columns the columns to add to the select. See [[select()]] for more |
||
604 | * details about the format of this parameter. |
||
605 | * @return $this the query object itself |
||
606 | * @see select() |
||
607 | */ |
||
608 | 9 | public function addSelect($columns) |
|
609 | { |
||
610 | 9 | if ($columns instanceof Expression) { |
|
611 | 3 | $columns = [$columns]; |
|
612 | 9 | } elseif (!is_array($columns)) { |
|
613 | 3 | $columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY); |
|
614 | } |
||
615 | 9 | if ($this->select === null) { |
|
616 | 3 | $this->select = $columns; |
|
617 | } else { |
||
618 | 9 | $this->select = array_merge($this->select, $columns); |
|
619 | } |
||
620 | |||
621 | 9 | return $this; |
|
622 | } |
||
623 | |||
624 | /** |
||
625 | * Sets the value indicating whether to SELECT DISTINCT or not. |
||
626 | * @param bool $value whether to SELECT DISTINCT or not. |
||
627 | * @return $this the query object itself |
||
628 | */ |
||
629 | 6 | public function distinct($value = true) |
|
634 | |||
635 | /** |
||
636 | * Sets the FROM part of the query. |
||
637 | * @param string|array|Expression $tables the table(s) to be selected from. This can be either a string (e.g. `'user'`) |
||
638 | * or an array (e.g. `['user', 'profile']`) specifying one or several table names. |
||
639 | * Table names can contain schema prefixes (e.g. `'public.user'`) and/or table aliases (e.g. `'user u'`). |
||
640 | * The method will automatically quote the table names unless it contains some parenthesis |
||
641 | * (which means the table is given as a sub-query or DB expression). |
||
642 | * |
||
643 | * When the tables are specified as an array, you may also use the array keys as the table aliases |
||
644 | * (if a table does not need alias, do not use a string key). |
||
645 | * |
||
646 | * Use a Query object to represent a sub-query. In this case, the corresponding array key will be used |
||
647 | * as the alias for the sub-query. |
||
648 | * |
||
649 | * To specify the `FROM` part in plain SQL, you may pass an instance of [[Expression]]. |
||
650 | * |
||
651 | * Here are some examples: |
||
652 | * |
||
653 | * ```php |
||
654 | * // SELECT * FROM `user` `u`, `profile`; |
||
655 | * $query = (new \yii\db\Query)->from(['u' => 'user', 'profile']); |
||
656 | * |
||
657 | * // SELECT * FROM (SELECT * FROM `user` WHERE `active` = 1) `activeusers`; |
||
658 | * $subquery = (new \yii\db\Query)->from('user')->where(['active' => true]) |
||
659 | * $query = (new \yii\db\Query)->from(['activeusers' => $subquery]); |
||
660 | * |
||
661 | * // subquery can also be a string with plain SQL wrapped in parenthesis |
||
662 | * // SELECT * FROM (SELECT * FROM `user` WHERE `active` = 1) `activeusers`; |
||
663 | * $subquery = "(SELECT * FROM `user` WHERE `active` = 1)"; |
||
664 | * $query = (new \yii\db\Query)->from(['activeusers' => $subquery]); |
||
665 | * ``` |
||
666 | * |
||
667 | * @return $this the query object itself |
||
668 | */ |
||
669 | 402 | public function from($tables) |
|
677 | |||
678 | /** |
||
679 | * Sets the WHERE part of the query. |
||
680 | * |
||
681 | * The method requires a `$condition` parameter, and optionally a `$params` parameter |
||
682 | * specifying the values to be bound to the query. |
||
683 | * |
||
684 | * The `$condition` parameter should be either a string (e.g. `'id=1'`) or an array. |
||
685 | * |
||
686 | * @inheritdoc |
||
687 | * |
||
688 | * @param string|array|Expression $condition the conditions that should be put in the WHERE part. |
||
689 | * @param array $params the parameters (name => value) to be bound to the query. |
||
690 | * @return $this the query object itself |
||
691 | * @see andWhere() |
||
692 | * @see orWhere() |
||
693 | * @see QueryInterface::where() |
||
694 | */ |
||
695 | 638 | public function where($condition, $params = []) |
|
701 | |||
702 | /** |
||
703 | * Adds an additional WHERE condition to the existing one. |
||
704 | * The new condition and the existing one will be joined using the `AND` operator. |
||
705 | * @param string|array|Expression $condition the new WHERE condition. Please refer to [[where()]] |
||
706 | * on how to specify this parameter. |
||
707 | * @param array $params the parameters (name => value) to be bound to the query. |
||
708 | * @return $this the query object itself |
||
709 | * @see where() |
||
710 | * @see orWhere() |
||
711 | */ |
||
712 | 322 | public function andWhere($condition, $params = []) |
|
724 | |||
725 | /** |
||
726 | * Adds an additional WHERE condition to the existing one. |
||
727 | * The new condition and the existing one will be joined using the `OR` operator. |
||
728 | * @param string|array|Expression $condition the new WHERE condition. Please refer to [[where()]] |
||
729 | * on how to specify this parameter. |
||
730 | * @param array $params the parameters (name => value) to be bound to the query. |
||
731 | * @return $this the query object itself |
||
732 | * @see where() |
||
733 | * @see andWhere() |
||
734 | */ |
||
735 | 7 | public function orWhere($condition, $params = []) |
|
745 | |||
746 | /** |
||
747 | * Adds a filtering condition for a specific column and allow the user to choose a filter operator. |
||
748 | * |
||
749 | * It adds an additional WHERE condition for the given field and determines the comparison operator |
||
750 | * based on the first few characters of the given value. |
||
751 | * The condition is added in the same way as in [[andFilterWhere]] so [[isEmpty()|empty values]] are ignored. |
||
752 | * The new condition and the existing one will be joined using the `AND` operator. |
||
753 | * |
||
754 | * The comparison operator is intelligently determined based on the first few characters in the given value. |
||
755 | * In particular, it recognizes the following operators if they appear as the leading characters in the given value: |
||
756 | * |
||
757 | * - `<`: the column must be less than the given value. |
||
758 | * - `>`: the column must be greater than the given value. |
||
759 | * - `<=`: the column must be less than or equal to the given value. |
||
760 | * - `>=`: the column must be greater than or equal to the given value. |
||
761 | * - `<>`: the column must not be the same as the given value. |
||
762 | * - `=`: the column must be equal to the given value. |
||
763 | * - If none of the above operators is detected, the `$defaultOperator` will be used. |
||
764 | * |
||
765 | * @param string $name the column name. |
||
766 | * @param string $value the column value optionally prepended with the comparison operator. |
||
767 | * @param string $defaultOperator The operator to use, when no operator is given in `$value`. |
||
768 | * Defaults to `=`, performing an exact match. |
||
769 | * @return $this The query object itself |
||
770 | * @since 2.0.8 |
||
771 | */ |
||
772 | 3 | public function andFilterCompare($name, $value, $defaultOperator = '=') |
|
783 | |||
784 | /** |
||
785 | * Appends a JOIN part to the query. |
||
786 | * The first parameter specifies what type of join it is. |
||
787 | * @param string $type the type of join, such as INNER JOIN, LEFT JOIN. |
||
788 | * @param string|array $table the table to be joined. |
||
789 | * |
||
790 | * Use a string to represent the name of the table to be joined. |
||
791 | * The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). |
||
792 | * The method will automatically quote the table name unless it contains some parenthesis |
||
793 | * (which means the table is given as a sub-query or DB expression). |
||
794 | * |
||
795 | * Use an array to represent joining with a sub-query. The array must contain only one element. |
||
796 | * The value must be a [[Query]] object representing the sub-query while the corresponding key |
||
797 | * represents the alias for the sub-query. |
||
798 | * |
||
799 | * @param string|array $on the join condition that should appear in the ON part. |
||
800 | * Please refer to [[where()]] on how to specify this parameter. |
||
801 | * |
||
802 | * Note that the array format of [[where()]] is designed to match columns to values instead of columns to columns, so |
||
803 | * the following would **not** work as expected: `['post.author_id' => 'user.id']`, it would |
||
804 | * match the `post.author_id` column value against the string `'user.id'`. |
||
805 | * It is recommended to use the string syntax here which is more suited for a join: |
||
806 | * |
||
807 | * ```php |
||
808 | * 'post.author_id = user.id' |
||
809 | * ``` |
||
810 | * |
||
811 | * @param array $params the parameters (name => value) to be bound to the query. |
||
812 | * @return $this the query object itself |
||
813 | */ |
||
814 | 48 | public function join($type, $table, $on = '', $params = []) |
|
819 | |||
820 | /** |
||
821 | * Appends an INNER JOIN part to the query. |
||
822 | * @param string|array $table the table to be joined. |
||
823 | * |
||
824 | * Use a string to represent the name of the table to be joined. |
||
825 | * The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). |
||
826 | * The method will automatically quote the table name unless it contains some parenthesis |
||
827 | * (which means the table is given as a sub-query or DB expression). |
||
828 | * |
||
829 | * Use an array to represent joining with a sub-query. The array must contain only one element. |
||
830 | * The value must be a [[Query]] object representing the sub-query while the corresponding key |
||
831 | * represents the alias for the sub-query. |
||
832 | * |
||
833 | * @param string|array $on the join condition that should appear in the ON part. |
||
834 | * Please refer to [[join()]] on how to specify this parameter. |
||
835 | * @param array $params the parameters (name => value) to be bound to the query. |
||
836 | * @return $this the query object itself |
||
837 | */ |
||
838 | 3 | public function innerJoin($table, $on = '', $params = []) |
|
843 | |||
844 | /** |
||
845 | * Appends a LEFT OUTER JOIN part to the query. |
||
846 | * @param string|array $table the table to be joined. |
||
847 | * |
||
848 | * Use a string to represent the name of the table to be joined. |
||
849 | * The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). |
||
850 | * The method will automatically quote the table name unless it contains some parenthesis |
||
851 | * (which means the table is given as a sub-query or DB expression). |
||
852 | * |
||
853 | * Use an array to represent joining with a sub-query. The array must contain only one element. |
||
854 | * The value must be a [[Query]] object representing the sub-query while the corresponding key |
||
855 | * represents the alias for the sub-query. |
||
856 | * |
||
857 | * @param string|array $on the join condition that should appear in the ON part. |
||
858 | * Please refer to [[join()]] on how to specify this parameter. |
||
859 | * @param array $params the parameters (name => value) to be bound to the query |
||
860 | * @return $this the query object itself |
||
861 | */ |
||
862 | 3 | public function leftJoin($table, $on = '', $params = []) |
|
867 | |||
868 | /** |
||
869 | * Appends a RIGHT OUTER JOIN part to the query. |
||
870 | * @param string|array $table the table to be joined. |
||
871 | * |
||
872 | * Use a string to represent the name of the table to be joined. |
||
873 | * The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). |
||
874 | * The method will automatically quote the table name unless it contains some parenthesis |
||
875 | * (which means the table is given as a sub-query or DB expression). |
||
876 | * |
||
877 | * Use an array to represent joining with a sub-query. The array must contain only one element. |
||
878 | * The value must be a [[Query]] object representing the sub-query while the corresponding key |
||
879 | * represents the alias for the sub-query. |
||
880 | * |
||
881 | * @param string|array $on the join condition that should appear in the ON part. |
||
882 | * Please refer to [[join()]] on how to specify this parameter. |
||
883 | * @param array $params the parameters (name => value) to be bound to the query |
||
884 | * @return $this the query object itself |
||
885 | */ |
||
886 | public function rightJoin($table, $on = '', $params = []) |
||
891 | |||
892 | /** |
||
893 | * Sets the GROUP BY part of the query. |
||
894 | * @param string|array|Expression $columns the columns to be grouped by. |
||
895 | * Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']). |
||
896 | * The method will automatically quote the column names unless a column contains some parenthesis |
||
897 | * (which means the column contains a DB expression). |
||
898 | * |
||
899 | * Note that if your group-by is an expression containing commas, you should always use an array |
||
900 | * to represent the group-by information. Otherwise, the method will not be able to correctly determine |
||
901 | * the group-by columns. |
||
902 | * |
||
903 | * Since version 2.0.7, an [[Expression]] object can be passed to specify the GROUP BY part explicitly in plain SQL. |
||
904 | * @return $this the query object itself |
||
905 | * @see addGroupBy() |
||
906 | */ |
||
907 | 24 | public function groupBy($columns) |
|
917 | |||
918 | /** |
||
919 | * Adds additional group-by columns to the existing ones. |
||
920 | * @param string|array $columns additional columns to be grouped by. |
||
921 | * Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']). |
||
922 | * The method will automatically quote the column names unless a column contains some parenthesis |
||
923 | * (which means the column contains a DB expression). |
||
924 | * |
||
925 | * Note that if your group-by is an expression containing commas, you should always use an array |
||
926 | * to represent the group-by information. Otherwise, the method will not be able to correctly determine |
||
927 | * the group-by columns. |
||
928 | * |
||
929 | * Since version 2.0.7, an [[Expression]] object can be passed to specify the GROUP BY part explicitly in plain SQL. |
||
930 | * @return $this the query object itself |
||
931 | * @see groupBy() |
||
932 | */ |
||
933 | 3 | public function addGroupBy($columns) |
|
948 | |||
949 | /** |
||
950 | * Sets the HAVING part of the query. |
||
951 | * @param string|array|Expression $condition the conditions to be put after HAVING. |
||
952 | * Please refer to [[where()]] on how to specify this parameter. |
||
953 | * @param array $params the parameters (name => value) to be bound to the query. |
||
954 | * @return $this the query object itself |
||
955 | * @see andHaving() |
||
956 | * @see orHaving() |
||
957 | */ |
||
958 | 10 | public function having($condition, $params = []) |
|
964 | |||
965 | /** |
||
966 | * Adds an additional HAVING condition to the existing one. |
||
967 | * The new condition and the existing one will be joined using the `AND` operator. |
||
968 | * @param string|array|Expression $condition the new HAVING condition. Please refer to [[where()]] |
||
969 | * on how to specify this parameter. |
||
970 | * @param array $params the parameters (name => value) to be bound to the query. |
||
971 | * @return $this the query object itself |
||
972 | * @see having() |
||
973 | * @see orHaving() |
||
974 | */ |
||
975 | 3 | public function andHaving($condition, $params = []) |
|
985 | |||
986 | /** |
||
987 | * Adds an additional HAVING condition to the existing one. |
||
988 | * The new condition and the existing one will be joined using the `OR` operator. |
||
989 | * @param string|array|Expression $condition the new HAVING condition. Please refer to [[where()]] |
||
990 | * on how to specify this parameter. |
||
991 | * @param array $params the parameters (name => value) to be bound to the query. |
||
992 | * @return $this the query object itself |
||
993 | * @see having() |
||
994 | * @see andHaving() |
||
995 | */ |
||
996 | 3 | public function orHaving($condition, $params = []) |
|
1006 | |||
1007 | /** |
||
1008 | * Sets the HAVING part of the query but ignores [[isEmpty()|empty operands]]. |
||
1009 | * |
||
1010 | * This method is similar to [[having()]]. The main difference is that this method will |
||
1011 | * remove [[isEmpty()|empty query operands]]. As a result, this method is best suited |
||
1012 | * for building query conditions based on filter values entered by users. |
||
1013 | * |
||
1014 | * The following code shows the difference between this method and [[having()]]: |
||
1015 | * |
||
1016 | * ```php |
||
1017 | * // HAVING `age`=:age |
||
1018 | * $query->filterHaving(['name' => null, 'age' => 20]); |
||
1019 | * // HAVING `age`=:age |
||
1020 | * $query->having(['age' => 20]); |
||
1021 | * // HAVING `name` IS NULL AND `age`=:age |
||
1022 | * $query->having(['name' => null, 'age' => 20]); |
||
1023 | * ``` |
||
1024 | * |
||
1025 | * Note that unlike [[having()]], you cannot pass binding parameters to this method. |
||
1026 | * |
||
1027 | * @param array $condition the conditions that should be put in the HAVING part. |
||
1028 | * See [[having()]] on how to specify this parameter. |
||
1029 | * @return $this the query object itself |
||
1030 | * @see having() |
||
1031 | * @see andFilterHaving() |
||
1032 | * @see orFilterHaving() |
||
1033 | * @since 2.0.11 |
||
1034 | */ |
||
1035 | 6 | public function filterHaving(array $condition) |
|
1044 | |||
1045 | /** |
||
1046 | * Adds an additional HAVING condition to the existing one but ignores [[isEmpty()|empty operands]]. |
||
1047 | * The new condition and the existing one will be joined using the `AND` operator. |
||
1048 | * |
||
1049 | * This method is similar to [[andHaving()]]. The main difference is that this method will |
||
1050 | * remove [[isEmpty()|empty query operands]]. As a result, this method is best suited |
||
1051 | * for building query conditions based on filter values entered by users. |
||
1052 | * |
||
1053 | * @param array $condition the new HAVING condition. Please refer to [[having()]] |
||
1054 | * on how to specify this parameter. |
||
1055 | * @return $this the query object itself |
||
1056 | * @see filterHaving() |
||
1057 | * @see orFilterHaving() |
||
1058 | * @since 2.0.11 |
||
1059 | */ |
||
1060 | 6 | public function andFilterHaving(array $condition) |
|
1069 | |||
1070 | /** |
||
1071 | * Adds an additional HAVING condition to the existing one but ignores [[isEmpty()|empty operands]]. |
||
1072 | * The new condition and the existing one will be joined using the `OR` operator. |
||
1073 | * |
||
1074 | * This method is similar to [[orHaving()]]. The main difference is that this method will |
||
1075 | * remove [[isEmpty()|empty query operands]]. As a result, this method is best suited |
||
1076 | * for building query conditions based on filter values entered by users. |
||
1077 | * |
||
1078 | * @param array $condition the new HAVING condition. Please refer to [[having()]] |
||
1079 | * on how to specify this parameter. |
||
1080 | * @return $this the query object itself |
||
1081 | * @see filterHaving() |
||
1082 | * @see andFilterHaving() |
||
1083 | * @since 2.0.11 |
||
1084 | */ |
||
1085 | 6 | public function orFilterHaving(array $condition) |
|
1094 | |||
1095 | /** |
||
1096 | * Appends a SQL statement using UNION operator. |
||
1097 | * @param string|Query $sql the SQL statement to be appended using UNION |
||
1098 | * @param bool $all TRUE if using UNION ALL and FALSE if using UNION |
||
1099 | * @return $this the query object itself |
||
1100 | */ |
||
1101 | 10 | public function union($sql, $all = false) |
|
1106 | |||
1107 | /** |
||
1108 | * Sets the parameters to be bound to the query. |
||
1109 | * @param array $params list of query parameter values indexed by parameter placeholders. |
||
1110 | * For example, `[':name' => 'Dan', ':age' => 31]`. |
||
1111 | * @return $this the query object itself |
||
1112 | * @see addParams() |
||
1113 | */ |
||
1114 | 6 | public function params($params) |
|
1119 | |||
1120 | /** |
||
1121 | * Adds additional parameters to be bound to the query. |
||
1122 | * @param array $params list of query parameter values indexed by parameter placeholders. |
||
1123 | * For example, `[':name' => 'Dan', ':age' => 31]`. |
||
1124 | * @return $this the query object itself |
||
1125 | * @see params() |
||
1126 | */ |
||
1127 | 882 | public function addParams($params) |
|
1145 | |||
1146 | /** |
||
1147 | * Creates a new Query object and copies its property values from an existing one. |
||
1148 | * The properties being copies are the ones to be used by query builders. |
||
1149 | * @param Query $from the source query object |
||
1150 | * @return Query the new Query object |
||
1151 | */ |
||
1152 | 346 | public static function create($from) |
|
1171 | } |
||
1172 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.