Query.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db;
  8. use Yii;
  9. use yii\base\Component;
  10. /**
  11. * Query represents a SELECT SQL statement in a way that is independent of DBMS.
  12. *
  13. * Query provides a set of methods to facilitate the specification of different clauses
  14. * in a SELECT statement. These methods can be chained together.
  15. *
  16. * By calling [[createCommand()]], we can get a [[Command]] instance which can be further
  17. * used to perform/execute the DB query against a database.
  18. *
  19. * For example,
  20. *
  21. * ~~~
  22. * $query = new Query;
  23. * // compose the query
  24. * $query->select('id, name')
  25. * ->from('tbl_user')
  26. * ->limit(10);
  27. * // build and execute the query
  28. * $rows = $query->all();
  29. * // alternatively, you can create DB command and execute it
  30. * $command = $query->createCommand();
  31. * // $command->sql returns the actual SQL
  32. * $rows = $command->queryAll();
  33. * ~~~
  34. *
  35. * @author Qiang Xue <[email protected]>
  36. * @author Carsten Brandt <[email protected]>
  37. * @since 2.0
  38. */
  39. class Query extends Component implements QueryInterface
  40. {
  41. use QueryTrait;
  42. /**
  43. * @var array the columns being selected. For example, `['id', 'name']`.
  44. * This is used to construct the SELECT clause in a SQL statement. If not set, it means selecting all columns.
  45. * @see select()
  46. */
  47. public $select;
  48. /**
  49. * @var string additional option that should be appended to the 'SELECT' keyword. For example,
  50. * in MySQL, the option 'SQL_CALC_FOUND_ROWS' can be used.
  51. */
  52. public $selectOption;
  53. /**
  54. * @var boolean whether to select distinct rows of data only. If this is set true,
  55. * the SELECT clause would be changed to SELECT DISTINCT.
  56. */
  57. public $distinct;
  58. /**
  59. * @var array the table(s) to be selected from. For example, `['tbl_user', 'tbl_post']`.
  60. * This is used to construct the FROM clause in a SQL statement.
  61. * @see from()
  62. */
  63. public $from;
  64. /**
  65. * @var array how to group the query results. For example, `['company', 'department']`.
  66. * This is used to construct the GROUP BY clause in a SQL statement.
  67. */
  68. public $groupBy;
  69. /**
  70. * @var array how to join with other tables. Each array element represents the specification
  71. * of one join which has the following structure:
  72. *
  73. * ~~~
  74. * [$joinType, $tableName, $joinCondition]
  75. * ~~~
  76. *
  77. * For example,
  78. *
  79. * ~~~
  80. * [
  81. * ['INNER JOIN', 'tbl_user', 'tbl_user.id = author_id'],
  82. * ['LEFT JOIN', 'tbl_team', 'tbl_team.id = team_id'],
  83. * ]
  84. * ~~~
  85. */
  86. public $join;
  87. /**
  88. * @var string|array the condition to be applied in the GROUP BY clause.
  89. * It can be either a string or an array. Please refer to [[where()]] on how to specify the condition.
  90. */
  91. public $having;
  92. /**
  93. * @var array this is used to construct the UNION clause(s) in a SQL statement.
  94. * Each array element can be either a string or a [[Query]] object representing a sub-query.
  95. */
  96. public $union;
  97. /**
  98. * @var array list of query parameter values indexed by parameter placeholders.
  99. * For example, `[':name' => 'Dan', ':age' => 31]`.
  100. */
  101. public $params;
  102. /**
  103. * Creates a DB command that can be used to execute this query.
  104. * @param Connection $db the database connection used to generate the SQL statement.
  105. * If this parameter is not given, the `db` application component will be used.
  106. * @return Command the created DB command instance.
  107. */
  108. public function createCommand($db = null)
  109. {
  110. if ($db === null) {
  111. $db = Yii::$app->getDb();
  112. }
  113. list ($sql, $params) = $db->getQueryBuilder()->build($this);
  114. return $db->createCommand($sql, $params);
  115. }
  116. /**
  117. * Executes the query and returns all results as an array.
  118. * @param Connection $db the database connection used to generate the SQL statement.
  119. * If this parameter is not given, the `db` application component will be used.
  120. * @return array the query results. If the query results in nothing, an empty array will be returned.
  121. */
  122. public function all($db = null)
  123. {
  124. $rows = $this->createCommand($db)->queryAll();
  125. if ($this->indexBy === null) {
  126. return $rows;
  127. }
  128. $result = [];
  129. foreach ($rows as $row) {
  130. if (is_string($this->indexBy)) {
  131. $key = $row[$this->indexBy];
  132. } else {
  133. $key = call_user_func($this->indexBy, $row);
  134. }
  135. $result[$key] = $row;
  136. }
  137. return $result;
  138. }
  139. /**
  140. * Executes the query and returns a single row of result.
  141. * @param Connection $db the database connection used to generate the SQL statement.
  142. * If this parameter is not given, the `db` application component will be used.
  143. * @return array|boolean the first row (in terms of an array) of the query result. False is returned if the query
  144. * results in nothing.
  145. */
  146. public function one($db = null)
  147. {
  148. return $this->createCommand($db)->queryOne();
  149. }
  150. /**
  151. * Returns the query result as a scalar value.
  152. * The value returned will be the first column in the first row of the query results.
  153. * @param Connection $db the database connection used to generate the SQL statement.
  154. * If this parameter is not given, the `db` application component will be used.
  155. * @return string|boolean the value of the first column in the first row of the query result.
  156. * False is returned if the query result is empty.
  157. */
  158. public function scalar($db = null)
  159. {
  160. return $this->createCommand($db)->queryScalar();
  161. }
  162. /**
  163. * Executes the query and returns the first column of the result.
  164. * @param Connection $db the database connection used to generate the SQL statement.
  165. * If this parameter is not given, the `db` application component will be used.
  166. * @return array the first column of the query result. An empty array is returned if the query results in nothing.
  167. */
  168. public function column($db = null)
  169. {
  170. return $this->createCommand($db)->queryColumn();
  171. }
  172. /**
  173. * Returns the number of records.
  174. * @param string $q the COUNT expression. Defaults to '*'.
  175. * Make sure you properly quote column names in the expression.
  176. * @param Connection $db the database connection used to generate the SQL statement.
  177. * If this parameter is not given (or null), the `db` application component will be used.
  178. * @return integer number of records
  179. */
  180. public function count($q = '*', $db = null)
  181. {
  182. return $this->queryScalar("COUNT($q)", $db);
  183. }
  184. /**
  185. * Returns the sum of the specified column values.
  186. * @param string $q the column name or expression.
  187. * Make sure you properly quote column names in the expression.
  188. * @param Connection $db the database connection used to generate the SQL statement.
  189. * If this parameter is not given, the `db` application component will be used.
  190. * @return integer the sum of the specified column values
  191. */
  192. public function sum($q, $db = null)
  193. {
  194. return $this->queryScalar("SUM($q)", $db);
  195. }
  196. /**
  197. * Returns the average of the specified column values.
  198. * @param string $q the column name or expression.
  199. * Make sure you properly quote column names in the expression.
  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 integer the average of the specified column values.
  203. */
  204. public function average($q, $db = null)
  205. {
  206. return $this->queryScalar("AVG($q)", $db);
  207. }
  208. /**
  209. * Returns the minimum of the specified column values.
  210. * @param string $q the column name or expression.
  211. * Make sure you properly quote column names in the expression.
  212. * @param Connection $db the database connection used to generate the SQL statement.
  213. * If this parameter is not given, the `db` application component will be used.
  214. * @return integer the minimum of the specified column values.
  215. */
  216. public function min($q, $db = null)
  217. {
  218. return $this->queryScalar("MIN($q)", $db);
  219. }
  220. /**
  221. * Returns the maximum of the specified column values.
  222. * @param string $q the column name or expression.
  223. * Make sure you properly quote column names in the expression.
  224. * @param Connection $db the database connection used to generate the SQL statement.
  225. * If this parameter is not given, the `db` application component will be used.
  226. * @return integer the maximum of the specified column values.
  227. */
  228. public function max($q, $db = null)
  229. {
  230. return $this->queryScalar("MAX($q)", $db);
  231. }
  232. /**
  233. * Returns a value indicating whether the query result contains any row of data.
  234. * @param Connection $db the database connection used to generate the SQL statement.
  235. * If this parameter is not given, the `db` application component will be used.
  236. * @return boolean whether the query result contains any row of data.
  237. */
  238. public function exists($db = null)
  239. {
  240. $select = $this->select;
  241. $this->select = [new Expression('1')];
  242. $command = $this->createCommand($db);
  243. $this->select = $select;
  244. return $command->queryScalar() !== false;
  245. }
  246. /**
  247. * Queries a scalar value by setting [[select]] first.
  248. * Restores the value of select to make this query reusable.
  249. * @param string|Expression $selectExpression
  250. * @param Connection $db
  251. * @return bool|string
  252. */
  253. private function queryScalar($selectExpression, $db)
  254. {
  255. $select = $this->select;
  256. $limit = $this->limit;
  257. $offset = $this->offset;
  258. $this->select = [$selectExpression];
  259. $this->limit = null;
  260. $this->offset = null;
  261. $command = $this->createCommand($db);
  262. $this->select = $select;
  263. $this->limit = $limit;
  264. $this->offset = $offset;
  265. return $command->queryScalar();
  266. }
  267. /**
  268. * Sets the SELECT part of the query.
  269. * @param string|array $columns the columns to be selected.
  270. * Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']).
  271. * Columns can contain table prefixes (e.g. "tbl_user.id") and/or column aliases (e.g. "tbl_user.id AS user_id").
  272. * The method will automatically quote the column names unless a column contains some parenthesis
  273. * (which means the column contains a DB expression).
  274. *
  275. * Note that if you are selecting an expression like `CONCAT(first_name, ' ', last_name)`, you should
  276. * use an array to specify the columns. Otherwise, the expression may be incorrectly split into several parts.
  277. *
  278. * @param string $option additional option that should be appended to the 'SELECT' keyword. For example,
  279. * in MySQL, the option 'SQL_CALC_FOUND_ROWS' can be used.
  280. * @return static the query object itself
  281. */
  282. public function select($columns, $option = null)
  283. {
  284. if (!is_array($columns)) {
  285. $columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY);
  286. }
  287. $this->select = $columns;
  288. $this->selectOption = $option;
  289. return $this;
  290. }
  291. /**
  292. * Sets the value indicating whether to SELECT DISTINCT or not.
  293. * @param bool $value whether to SELECT DISTINCT or not.
  294. * @return static the query object itself
  295. */
  296. public function distinct($value = true)
  297. {
  298. $this->distinct = $value;
  299. return $this;
  300. }
  301. /**
  302. * Sets the FROM part of the query.
  303. * @param string|array $tables the table(s) to be selected from. This can be either a string (e.g. `'tbl_user'`)
  304. * or an array (e.g. `['tbl_user', 'tbl_profile']`) specifying one or several table names.
  305. * Table names can contain schema prefixes (e.g. `'public.tbl_user'`) and/or table aliases (e.g. `'tbl_user u'`).
  306. * The method will automatically quote the table names unless it contains some parenthesis
  307. * (which means the table is given as a sub-query or DB expression).
  308. * @return static the query object itself
  309. */
  310. public function from($tables)
  311. {
  312. if (!is_array($tables)) {
  313. $tables = preg_split('/\s*,\s*/', trim($tables), -1, PREG_SPLIT_NO_EMPTY);
  314. }
  315. $this->from = $tables;
  316. return $this;
  317. }
  318. /**
  319. * Sets the WHERE part of the query.
  320. *
  321. * The method requires a $condition parameter, and optionally a $params parameter
  322. * specifying the values to be bound to the query.
  323. *
  324. * The $condition parameter should be either a string (e.g. 'id=1') or an array.
  325. * If the latter, it must be in one of the following two formats:
  326. *
  327. * - hash format: `['column1' => value1, 'column2' => value2, ...]`
  328. * - operator format: `[operator, operand1, operand2, ...]`
  329. *
  330. * A condition in hash format represents the following SQL expression in general:
  331. * `column1=value1 AND column2=value2 AND ...`. In case when a value is an array,
  332. * an `IN` expression will be generated. And if a value is null, `IS NULL` will be used
  333. * in the generated expression. Below are some examples:
  334. *
  335. * - `['type' => 1, 'status' => 2]` generates `(type = 1) AND (status = 2)`.
  336. * - `['id' => [1, 2, 3], 'status' => 2]` generates `(id IN (1, 2, 3)) AND (status = 2)`.
  337. * - `['status' => null] generates `status IS NULL`.
  338. *
  339. * A condition in operator format generates the SQL expression according to the specified operator, which
  340. * can be one of the followings:
  341. *
  342. * - `and`: the operands should be concatenated together using `AND`. For example,
  343. * `['and', 'id=1', 'id=2']` will generate `id=1 AND id=2`. If an operand is an array,
  344. * it will be converted into a string using the rules described here. For example,
  345. * `['and', 'type=1', ['or', 'id=1', 'id=2']]` will generate `type=1 AND (id=1 OR id=2)`.
  346. * The method will NOT do any quoting or escaping.
  347. *
  348. * - `or`: similar to the `and` operator except that the operands are concatenated using `OR`.
  349. *
  350. * - `between`: operand 1 should be the column name, and operand 2 and 3 should be the
  351. * starting and ending values of the range that the column is in.
  352. * For example, `['between', 'id', 1, 10]` will generate `id BETWEEN 1 AND 10`.
  353. *
  354. * - `not between`: similar to `between` except the `BETWEEN` is replaced with `NOT BETWEEN`
  355. * in the generated condition.
  356. *
  357. * - `in`: operand 1 should be a column or DB expression, and operand 2 be an array representing
  358. * the range of the values that the column or DB expression should be in. For example,
  359. * `['in', 'id', [1, 2, 3]]` will generate `id IN (1, 2, 3)`.
  360. * The method will properly quote the column name and escape values in the range.
  361. *
  362. * - `not in`: similar to the `in` operator except that `IN` is replaced with `NOT IN` in the generated condition.
  363. *
  364. * - `like`: operand 1 should be a column or DB expression, and operand 2 be a string or an array representing
  365. * the values that the column or DB expression should be like.
  366. * For example, `['like', 'name', 'tester']` will generate `name LIKE '%tester%'`.
  367. * When the value range is given as an array, multiple `LIKE` predicates will be generated and concatenated
  368. * using `AND`. For example, `['like', 'name', ['test', 'sample']]` will generate
  369. * `name LIKE '%test%' AND name LIKE '%sample%'`.
  370. * The method will properly quote the column name and escape special characters in the values.
  371. * Sometimes, you may want to add the percentage characters to the matching value by yourself, you may supply
  372. * a third operand `false` to do so. For example, `['like', 'name', '%tester', false]` will generate `name LIKE '%tester'`.
  373. *
  374. * - `or like`: similar to the `like` operator except that `OR` is used to concatenate the `LIKE`
  375. * predicates when operand 2 is an array.
  376. *
  377. * - `not like`: similar to the `like` operator except that `LIKE` is replaced with `NOT LIKE`
  378. * in the generated condition.
  379. *
  380. * - `or not like`: similar to the `not like` operator except that `OR` is used to concatenate
  381. * the `NOT LIKE` predicates.
  382. *
  383. * - `exists`: requires one operand which must be an instance of [[Query]] representing the sub-query.
  384. * It will build a `EXISTS (sub-query)` expression.
  385. *
  386. * - `not exists`: similar to the `exists` operator and builds a `NOT EXISTS (sub-query)` expression.
  387. *
  388. * @param string|array $condition the conditions that should be put in the WHERE part.
  389. * @param array $params the parameters (name => value) to be bound to the query.
  390. * @return static the query object itself
  391. * @see andWhere()
  392. * @see orWhere()
  393. */
  394. public function where($condition, $params = [])
  395. {
  396. $this->where = $condition;
  397. $this->addParams($params);
  398. return $this;
  399. }
  400. /**
  401. * Adds an additional WHERE condition to the existing one.
  402. * The new condition and the existing one will be joined using the 'AND' operator.
  403. * @param string|array $condition the new WHERE condition. Please refer to [[where()]]
  404. * on how to specify this parameter.
  405. * @param array $params the parameters (name => value) to be bound to the query.
  406. * @return static the query object itself
  407. * @see where()
  408. * @see orWhere()
  409. */
  410. public function andWhere($condition, $params = [])
  411. {
  412. if ($this->where === null) {
  413. $this->where = $condition;
  414. } else {
  415. $this->where = ['and', $this->where, $condition];
  416. }
  417. $this->addParams($params);
  418. return $this;
  419. }
  420. /**
  421. * Adds an additional WHERE condition to the existing one.
  422. * The new condition and the existing one will be joined using the 'OR' operator.
  423. * @param string|array $condition the new WHERE condition. Please refer to [[where()]]
  424. * on how to specify this parameter.
  425. * @param array $params the parameters (name => value) to be bound to the query.
  426. * @return static the query object itself
  427. * @see where()
  428. * @see andWhere()
  429. */
  430. public function orWhere($condition, $params = [])
  431. {
  432. if ($this->where === null) {
  433. $this->where = $condition;
  434. } else {
  435. $this->where = ['or', $this->where, $condition];
  436. }
  437. $this->addParams($params);
  438. return $this;
  439. }
  440. /**
  441. * Appends a JOIN part to the query.
  442. * The first parameter specifies what type of join it is.
  443. * @param string $type the type of join, such as INNER JOIN, LEFT JOIN.
  444. * @param string $table the table to be joined.
  445. * Table name can contain schema prefix (e.g. 'public.tbl_user') and/or table alias (e.g. 'tbl_user u').
  446. * The method will automatically quote the table name unless it contains some parenthesis
  447. * (which means the table is given as a sub-query or DB expression).
  448. * @param string|array $on the join condition that should appear in the ON part.
  449. * Please refer to [[where()]] on how to specify this parameter.
  450. * @param array $params the parameters (name => value) to be bound to the query.
  451. * @return Query the query object itself
  452. */
  453. public function join($type, $table, $on = '', $params = [])
  454. {
  455. $this->join[] = [$type, $table, $on];
  456. return $this->addParams($params);
  457. }
  458. /**
  459. * Appends an INNER JOIN part to the query.
  460. * @param string $table the table to be joined.
  461. * Table name can contain schema prefix (e.g. 'public.tbl_user') and/or table alias (e.g. 'tbl_user u').
  462. * The method will automatically quote the table name unless it contains some parenthesis
  463. * (which means the table is given as a sub-query or DB expression).
  464. * @param string|array $on the join condition that should appear in the ON part.
  465. * Please refer to [[where()]] on how to specify this parameter.
  466. * @param array $params the parameters (name => value) to be bound to the query.
  467. * @return Query the query object itself
  468. */
  469. public function innerJoin($table, $on = '', $params = [])
  470. {
  471. $this->join[] = ['INNER JOIN', $table, $on];
  472. return $this->addParams($params);
  473. }
  474. /**
  475. * Appends a LEFT OUTER JOIN part to the query.
  476. * @param string $table the table to be joined.
  477. * Table name can contain schema prefix (e.g. 'public.tbl_user') and/or table alias (e.g. 'tbl_user u').
  478. * The method will automatically quote the table name unless it contains some parenthesis
  479. * (which means the table is given as a sub-query or DB expression).
  480. * @param string|array $on the join condition that should appear in the ON part.
  481. * Please refer to [[where()]] on how to specify this parameter.
  482. * @param array $params the parameters (name => value) to be bound to the query
  483. * @return Query the query object itself
  484. */
  485. public function leftJoin($table, $on = '', $params = [])
  486. {
  487. $this->join[] = ['LEFT JOIN', $table, $on];
  488. return $this->addParams($params);
  489. }
  490. /**
  491. * Appends a RIGHT OUTER JOIN part to the query.
  492. * @param string $table the table to be joined.
  493. * Table name can contain schema prefix (e.g. 'public.tbl_user') and/or table alias (e.g. 'tbl_user u').
  494. * The method will automatically quote the table name unless it contains some parenthesis
  495. * (which means the table is given as a sub-query or DB expression).
  496. * @param string|array $on the join condition that should appear in the ON part.
  497. * Please refer to [[where()]] on how to specify this parameter.
  498. * @param array $params the parameters (name => value) to be bound to the query
  499. * @return Query the query object itself
  500. */
  501. public function rightJoin($table, $on = '', $params = [])
  502. {
  503. $this->join[] = ['RIGHT JOIN', $table, $on];
  504. return $this->addParams($params);
  505. }
  506. /**
  507. * Sets the GROUP BY part of the query.
  508. * @param string|array $columns the columns to be grouped by.
  509. * Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']).
  510. * The method will automatically quote the column names unless a column contains some parenthesis
  511. * (which means the column contains a DB expression).
  512. * @return static the query object itself
  513. * @see addGroupBy()
  514. */
  515. public function groupBy($columns)
  516. {
  517. if (!is_array($columns)) {
  518. $columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY);
  519. }
  520. $this->groupBy = $columns;
  521. return $this;
  522. }
  523. /**
  524. * Adds additional group-by columns to the existing ones.
  525. * @param string|array $columns additional columns to be grouped by.
  526. * Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']).
  527. * The method will automatically quote the column names unless a column contains some parenthesis
  528. * (which means the column contains a DB expression).
  529. * @return static the query object itself
  530. * @see groupBy()
  531. */
  532. public function addGroupBy($columns)
  533. {
  534. if (!is_array($columns)) {
  535. $columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY);
  536. }
  537. if ($this->groupBy === null) {
  538. $this->groupBy = $columns;
  539. } else {
  540. $this->groupBy = array_merge($this->groupBy, $columns);
  541. }
  542. return $this;
  543. }
  544. /**
  545. * Sets the HAVING part of the query.
  546. * @param string|array $condition the conditions to be put after HAVING.
  547. * Please refer to [[where()]] on how to specify this parameter.
  548. * @param array $params the parameters (name => value) to be bound to the query.
  549. * @return static the query object itself
  550. * @see andHaving()
  551. * @see orHaving()
  552. */
  553. public function having($condition, $params = [])
  554. {
  555. $this->having = $condition;
  556. $this->addParams($params);
  557. return $this;
  558. }
  559. /**
  560. * Adds an additional HAVING condition to the existing one.
  561. * The new condition and the existing one will be joined using the 'AND' operator.
  562. * @param string|array $condition the new HAVING 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 static the query object itself
  566. * @see having()
  567. * @see orHaving()
  568. */
  569. public function andHaving($condition, $params = [])
  570. {
  571. if ($this->having === null) {
  572. $this->having = $condition;
  573. } else {
  574. $this->having = ['and', $this->having, $condition];
  575. }
  576. $this->addParams($params);
  577. return $this;
  578. }
  579. /**
  580. * Adds an additional HAVING condition to the existing one.
  581. * The new condition and the existing one will be joined using the 'OR' operator.
  582. * @param string|array $condition the new HAVING condition. Please refer to [[where()]]
  583. * on how to specify this parameter.
  584. * @param array $params the parameters (name => value) to be bound to the query.
  585. * @return static the query object itself
  586. * @see having()
  587. * @see andHaving()
  588. */
  589. public function orHaving($condition, $params = [])
  590. {
  591. if ($this->having === null) {
  592. $this->having = $condition;
  593. } else {
  594. $this->having = ['or', $this->having, $condition];
  595. }
  596. $this->addParams($params);
  597. return $this;
  598. }
  599. /**
  600. * Appends a SQL statement using UNION operator.
  601. * @param string|Query $sql the SQL statement to be appended using UNION
  602. * @return static the query object itself
  603. */
  604. public function union($sql)
  605. {
  606. $this->union[] = $sql;
  607. return $this;
  608. }
  609. /**
  610. * Sets the parameters to be bound to the query.
  611. * @param array $params list of query parameter values indexed by parameter placeholders.
  612. * For example, `[':name' => 'Dan', ':age' => 31]`.
  613. * @return static the query object itself
  614. * @see addParams()
  615. */
  616. public function params($params)
  617. {
  618. $this->params = $params;
  619. return $this;
  620. }
  621. /**
  622. * Adds additional parameters to be bound to the query.
  623. * @param array $params list of query parameter values indexed by parameter placeholders.
  624. * For example, `[':name' => 'Dan', ':age' => 31]`.
  625. * @return static the query object itself
  626. * @see params()
  627. */
  628. public function addParams($params)
  629. {
  630. if (!empty($params)) {
  631. if ($this->params === null) {
  632. $this->params = $params;
  633. } else {
  634. foreach ($params as $name => $value) {
  635. if (is_integer($name)) {
  636. $this->params[] = $value;
  637. } else {
  638. $this->params[$name] = $value;
  639. }
  640. }
  641. }
  642. }
  643. return $this;
  644. }
  645. }