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 |
||
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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; |
|
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) |
|
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 = []) |
|
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 = []) |
|
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 = []) |
|
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 = []) |
|
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 = []) |
||
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 = []) |
||
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 = []) |
||
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) |
|
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) |
|
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 = []) |
|
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 = []) |
|
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 = []) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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.