Interface Yiisoft\Db\Query\QueryInterface
| Extends | Yiisoft\Db\Expression\ExpressionInterface, Yiisoft\Db\Query\QueryFunctionsInterface, Yiisoft\Db\Query\QueryPartsInterface |
|---|---|
| Implemented by | Yiisoft\Db\Query\Query |
Defines several methods for building and executing database queries, including methods for selecting data, inserting data, updating data, and deleting data.
It also defines methods for specifying the conditions for a query, as well as methods for pagination and sorting.
It has support for getting {@see \Yiisoft\Db\Query\one()} instance or {@see \Yiisoft\Db\Query\all()}.
Allows pagination via {@see \Yiisoft\Db\Query\limit()} and {@see \Yiisoft\Db\Query\offset()}.
Sorting is supported via {@see \Yiisoft\Db\Query\orderBy()} and items can be limited to match some conditions using {@see \Yiisoft\Db\Query\where()}.
Psalm Types
Public Methods
Method Details
Defined in: Yiisoft\Db\Query\QueryPartsInterface::addFor()
Adds more FOR parts to the existing ones.
See also:
| public abstract Yiisoft\Db\Query\QueryInterface addFor ( array|string|null $value ) | ||
| $value | array|string|null |
The value(s) to be set for the |
public function addFor(string|array|null $value): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::addGroupBy()
Adds more group-by columns to the existing ones.
See also groupBy().
| public abstract Yiisoft\Db\Query\QueryInterface addGroupBy ( array|Yiisoft\Db\Expression\ExpressionInterface|string $columns ) | ||
| $columns | array|Yiisoft\Db\Expression\ExpressionInterface|string |
More columns to be grouped by. Columns can be specified in either a string (for example 'id, name') or an array (such as ['id', 'name']). The method will automatically quote the column names unless a column has some parenthesis (which means the column has a DB expression). Note that if your group-by is an expression containing commas, you should always use an array to represent the group-by information. Otherwise, the method won't be able to correctly decide the group-by columns. {@see \Yiisoft\Db\Expression\ExpressionInterface} object can be passed to specify the |
public function addGroupBy(array|string|ExpressionInterface $columns): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::addOrderBy()
Adds more ORDER BY columns to the query.
See also orderBy().
| public abstract Yiisoft\Db\Query\QueryInterface addOrderBy ( array|Yiisoft\Db\Expression\ExpressionInterface|string $columns ) | ||
| $columns | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The columns (and the directions) to be ordered by.
Columns can be specified in either a string (for example "id ASC, name DESC") or an array
(for example, |
public function addOrderBy(array|string|ExpressionInterface $columns): static;
Adds more parameters to bind to the query.
See also params().
| public abstract Yiisoft\Db\Query\QueryInterface addParams ( array $params ) | ||
| $params | array |
The list of query parameter values indexed by parameter placeholders.
For example, |
public function addParams(array $params): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::addSelect()
Add more columns to the SELECT part of the query.
Note, that if {@see \Yiisoft\Db\Query\select} hasn't been specified before, you should include * explicitly if you want to select
all remaining columns too:
$query->addSelect(["*", "CONCAT(first_name, ' ', last_name) AS full_name"])->one();
See also select() for more details about the format of this parameter.
| public abstract Yiisoft\Db\Query\QueryInterface addSelect ( array|Yiisoft\Db\Expression\ExpressionInterface|scalar $columns ) | ||
| $columns | array|Yiisoft\Db\Expression\ExpressionInterface|scalar |
The columns to add to the select. |
| Version | Description |
|---|---|
| 2.0.0 | `$columns` can be a scalar value or an array of scalar values. |
public function addSelect(array|bool|float|int|string|ExpressionInterface $columns): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::addWithQuery()
Prepends an SQL statement using WITH syntax.
| public abstract Yiisoft\Db\Query\QueryInterface addWithQuery ( Yiisoft\Db\Query\QueryInterface|string $query, Yiisoft\Db\Expression\ExpressionInterface|string $alias, boolean $recursive = false ) | ||
| $query | Yiisoft\Db\Query\QueryInterface|string |
The SQL statement to append using |
| $alias | Yiisoft\Db\Expression\ExpressionInterface|string |
The query alias in |
| $recursive | boolean |
Its |
public function addWithQuery(
QueryInterface|string $query,
ExpressionInterface|string $alias,
bool $recursive = false,
): static;
Executes the query and returns all results as an array.
| public abstract array[]|object[] all ( ) | ||
| return | array[]|object[] |
All rows of the query result. Each array element is an |
|---|---|---|
| throws | Yiisoft\Db\Exception\Exception | |
| throws | Yiisoft\Db\Exception\InvalidConfigException | |
| throws | Throwable | |
public function all(): array;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::andFilterCompare()
Adds a filtering condition for a specific column and allow the user to choose a filter operator.
It adds WHERE condition for the given field and determines the comparison operator based on the first few
characters of the given value.
The condition is added in the same way as in {@see \Yiisoft\Db\Query\andFilterWhere()} so {@see \Yiisoft\Db\Query\Query::isEmpty()} are ignored.
The new condition and the existing one are joined using the AND operator.
The comparison operator is intelligently determined based on the first few characters in the given value.
In particular, it recognizes the following operators if they appear as the leading characters in the given value:
<: the column must be less than the given value.>: the column must be greater than the given value.<=: the column must be less than or equal to the given value.>=: the column must be greater than or equal to the given value.<>: the column must not be the same as the given value.=: the column must be equal to the given value.- If operator isn't regognized, the
$defaultOperatoris used.
| public abstract Yiisoft\Db\Query\QueryInterface andFilterCompare ( string $column, string|null $value, string $defaultOperator = '=' ) | ||
| $column | string |
The column name. |
| $value | string|null |
The column value optionally prepended with the comparison operator. |
| $defaultOperator | string |
The operator to use when no operator is given in |
| throws | Yiisoft\Db\Exception\NotSupportedException |
If this query doesn't support filtering. |
|---|---|---|
public function andFilterCompare(string $column, ?string $value, string $defaultOperator = '='): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::andFilterHaving()
Adds HAVING condition to the existing one but ignores {@see Query::isEmpty()}.
The new condition and the existing one will be joined using the AND operator.
This method is similar to {@see \Yiisoft\Db\Query\andHaving()}. The main difference is that this method will remove {@see \Yiisoft\Db\Query\Query::isEmpty()}.
As a result, this method is best suited for building query conditions based on filter values entered by users.
See also:
| public abstract Yiisoft\Db\Query\QueryInterface andFilterHaving ( array $condition ) | ||
| $condition | array |
The new |
| throws | Yiisoft\Db\Exception\NotSupportedException |
If this query doesn't support filtering. |
|---|---|---|
public function andFilterHaving(array $condition): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::andFilterWhere()
Adds WHERE condition to the existing one but ignores {@see Query::isEmpty()}.
The new condition and the existing one will be joined using the AND operator.
This method is similar to {@see \Yiisoft\Db\Query\andWhere()}. The main difference is that this method will remove {@see \Yiisoft\Db\Query\Query::isEmpty()}.
As a result, this method is best suited for building query conditions based on filter values entered by users.
See also:
| public abstract Yiisoft\Db\Query\QueryInterface andFilterWhere ( array $condition ) | ||
| $condition | array |
The new |
| throws | Yiisoft\Db\Exception\NotSupportedException |
If this query doesn't support filtering. |
|---|---|---|
public function andFilterWhere(array $condition): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::andHaving()
Adds HAVING condition to the existing one.
The new condition and the existing one will be joined using the AND operator.
See also:
| public abstract Yiisoft\Db\Query\QueryInterface andHaving ( array|Yiisoft\Db\Expression\ExpressionInterface|string $condition, array $params = [] ) | ||
| $condition | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The new HAVING condition. Please refer to {@see \Yiisoft\Db\Query\where()} on how to specify this parameter. |
| $params | array |
The parameters (name => value) to be bound to the query. |
public function andHaving(array|string|ExpressionInterface $condition, array $params = []): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::andWhere()
Adds WHERE condition to the existing one.
The new condition and the existing one will be joined using the AND operator.
See also:
| public abstract Yiisoft\Db\Query\QueryInterface andWhere ( array|Yiisoft\Db\Expression\ExpressionInterface|string $condition, array $params = [] ) | ||
| $condition | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The new |
| $params | array |
The parameters (name => value) to be bound to the query. |
public function andWhere(array|ExpressionInterface|string $condition, array $params = []): static;
Defined in: Yiisoft\Db\Query\QueryFunctionsInterface::average()
Returns the average of the specified column values.
| public abstract float|integer|string|null average ( string $sql ) | ||
| $sql | string |
The column name or expression. |
| return | float|integer|string|null |
The average of the specified column values. Note: Make sure you quote column names in the expression. |
|---|---|---|
| throws | Throwable | |
public function average(string $sql): int|float|string|null;
Starts a batch query.
A batch query supports fetching data in batches, which can keep the memory usage under a limit.
This method will return a {@see \Yiisoft\Db\Query\BatchQueryResultInterface} object which implements the {@see \Iterator} interface and can be traversed to retrieve the data in batches.
For example,
$query = (new Query)->from('user');
foreach ($query->batch() as $rows) {
// $rows is an array of 100 or fewer rows from user table
}
| public abstract Yiisoft\Db\Query\BatchQueryResultInterface batch ( integer $batchSize = 100 ) | ||
| $batchSize | integer |
The number of records to fetch in each batch. |
| return | Yiisoft\Db\Query\BatchQueryResultInterface |
The batch query result. It implements the {@see \Iterator} interface and can be traversed to retrieve the data in batches. |
|---|---|---|
public function batch(int $batchSize = 100): BatchQueryResultInterface;
Executes the query and returns the first column of the result.
| public abstract array column ( ) | ||
| return | array |
The first column of the query result. It returns an empty array if the query results in nothing. |
|---|---|---|
| throws | Yiisoft\Db\Exception\Exception | |
| throws | Yiisoft\Db\Exception\InvalidConfigException | |
| throws | Yiisoft\Db\Exception\NotSupportedException | |
| throws | Throwable | |
public function column(): array;
Defined in: Yiisoft\Db\Query\QueryFunctionsInterface::count()
Returns the number of records.
Note: Make sure you quote column names in the expression.
| public abstract integer|string count ( string $sql = '*' ) | ||
| $sql | string |
The |
| return | integer|string |
Number of records. The result will be a string when value is higher than {@see \Yiisoft\Db\Query\PHP_INT_MAX}. |
|---|---|---|
| throws | Yiisoft\Db\Exception\InvalidConfigException | |
| throws | Throwable | |
| throws | Yiisoft\Db\Exception\Exception | |
public function count(string $sql = '*'): int|string;
Creates a DB command to execute the query.
| public abstract Yiisoft\Db\Command\CommandInterface createCommand ( ) | ||
| return | Yiisoft\Db\Command\CommandInterface |
The created DB command instance. |
|---|---|---|
| throws | Yiisoft\Db\Exception\Exception | |
| throws | Yiisoft\Db\Exception\InvalidConfigException | |
public function createCommand(): CommandInterface;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::distinct()
Sets the value indicating whether to SELECT DISTINCT or not.
| public abstract Yiisoft\Db\Query\QueryInterface distinct ( boolean $value = true ) | ||
| $value | boolean |
Whether to |
public function distinct(bool $value = true): static;
Creates data reader to retrieve data row by row.
For example,
$query = (new Query)->from('user');
foreach ($query->each() as $row) {
}
| public abstract Yiisoft\Db\Query\DataReaderInterface each ( ) | ||
| return | Yiisoft\Db\Query\DataReaderInterface |
The data reader. It implements the {@see \Iterator} interface and can be traversed to retrieve each row of data. |
|---|---|---|
public function each(): DataReaderInterface;
Sets whether to emulate query execution without actually executing a query.
When enabled, methods returning results such as {@see \Yiisoft\Db\Query\one()}, {@see \Yiisoft\Db\Query\all()}, or {@see \Yiisoft\Db\Query\exists()}
will return empty or false values.
You should use this method in case your program logic requires that a query shouldn't return any results.
| public abstract Yiisoft\Db\Query\QueryInterface emulateExecution ( boolean $value = true ) | ||
| $value | boolean |
Whether to emulate query execution. |
public function emulateExecution(bool $value = true): static;
Returns a value indicating whether the query result has any row of data.
| public abstract boolean exists ( ) | ||
| return | boolean |
Whether the query result has any row of data. |
|---|---|---|
| throws | Yiisoft\Db\Exception\Exception | |
| throws | Yiisoft\Db\Exception\InvalidConfigException | |
| throws | Throwable | |
public function exists(): bool;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::filterHaving()
Sets the HAVING part of the query but ignores {@see Query::isEmpty()}.
This method is similar to {@see \Yiisoft\Db\Query\having()}. The main difference is that this method will remove {@see \Yiisoft\Db\Query\Query::isEmpty()}. As a result, this method is best suited for building query conditions based on filter values entered by users.
The following code shows the difference between this method and {@see \Yiisoft\Db\Query\having()}:
// HAVING `age`=:age
$query->filterHaving(['name' => null, 'age' => 20]);
// HAVING `age`=:age
$query->having(['age' => 20]);
// HAVING `name` IS NULL AND `age`=:age
$query->having(['name' => null, 'age' => 20]);
Note that unlike {@see \Yiisoft\Db\Query\having()}, you can't pass binding parameters to this method.
See also:
| public abstract Yiisoft\Db\Query\QueryInterface filterHaving ( array $condition ) | ||
| $condition | array |
The conditions that should be in the |
| throws | Yiisoft\Db\Exception\NotSupportedException |
If this query doesn't support filtering. |
|---|---|---|
public function filterHaving(array $condition): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::filterWhere()
Sets the WHERE part of the query but ignores {@see Query::isEmpty()}.
This method is similar to {@see \Yiisoft\Db\Query\where()}.
The main difference is that this method will remove {@see \Yiisoft\Db\Query\Query::isEmpty()}.
As a result, this method is best suited for building query conditions based on filter values entered by users.
The following code shows the difference between this method and {@see \Yiisoft\Db\Query\where()}:
// WHERE `age`=:age
$query->filterWhere(['name' => null, 'age' => 20]);
// WHERE `age`=:age
$query->where(['age' => 20]);
// WHERE `name` IS NULL AND `age`=:age
$query->where(['name' => null, 'age' => 20]);
Note that unlike {@see \Yiisoft\Db\Query\where()}, you can't pass binding parameters to this method.
See also:
| public abstract Yiisoft\Db\Query\QueryInterface filterWhere ( array $condition ) | ||
| $condition | array |
The conditions that should be in the |
| throws | Yiisoft\Db\Exception\NotSupportedException |
If this query doesn't support filtering. |
|---|---|---|
public function filterWhere(array $condition): static;
| public abstract Yiisoft\Db\Query\QueryInterface for ( array|string|null $value ) | ||
| $value | array|string|null |
The value(s) to be set for the |
| throws | LogicException |
If |
|---|---|---|
public function for(string|array|null $value): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::from()
Sets the FROM part of the query.
| public abstract Yiisoft\Db\Query\QueryInterface from ( array|Yiisoft\Db\Expression\ExpressionInterface|string $tables ) | ||
| $tables | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The table(s) to select from.
This can be either a string (for example,
|
public function from(array|ExpressionInterface|string $tables): static;
| public abstract boolean getDistinct ( ) | ||
| return | boolean |
The "distinct" value. |
|---|---|---|
public function getDistinct(): bool;
| public abstract string[] getFor ( ) | ||
| return | string[] |
The "for" values. |
|---|---|---|
public function getFor(): array;
| public abstract array getGroupBy ( ) | ||
| return | array |
The "group by" value. |
|---|---|---|
public function getGroupBy(): array;
| public abstract array|Yiisoft\Db\Expression\ExpressionInterface|string|null getHaving ( ) | ||
| return | array|Yiisoft\Db\Expression\ExpressionInterface|string|null |
The "having" value. |
|---|---|---|
public function getHaving(): string|array|ExpressionInterface|null;
| public abstract Closure|string|null getIndexBy ( ) | ||
| return | Closure|string|null |
The "index by" value. |
|---|---|---|
public function getIndexBy(): Closure|string|null;
| public abstract array getJoins ( ) | ||
| return | array |
The "join" value. The format is:
|
|---|---|---|
public function getJoins(): array;
| public abstract Yiisoft\Db\Expression\ExpressionInterface|integer|null getLimit ( ) | ||
| return | Yiisoft\Db\Expression\ExpressionInterface|integer|null |
The "limit" value. |
|---|---|---|
public function getLimit(): ExpressionInterface|int|null;
| public abstract Yiisoft\Db\Expression\ExpressionInterface|integer|null getOffset ( ) | ||
| return | Yiisoft\Db\Expression\ExpressionInterface|integer|null |
The "offset" value. |
|---|---|---|
public function getOffset(): ExpressionInterface|int|null;
| public abstract array getOrderBy ( ) | ||
| return | array |
The "order by" value. |
|---|---|---|
public function getOrderBy(): array;
| public abstract array getParams ( ) | ||
| return | array |
The "params" value. |
|---|---|---|
public function getParams(): array;
Returns the callback to be called on all rows of the query result.
null will be returned if the callback is not set.
| public abstract Closure|null getResultCallback ( ) |
public function getResultCallback(): ?Closure;
| public abstract array getSelect ( ) | ||
| return | array |
The "select" value. |
|---|---|---|
public function getSelect(): array;
| public abstract string|null getSelectOption ( ) | ||
| return | string|null |
The "select option" value. |
|---|---|---|
public function getSelectOption(): ?string;
Returns table names used in {@see from()} indexed by aliases.
Both aliases and names are enclosed into {{ and }}.
| public abstract array getTablesUsedInFrom ( ) | ||
| return | array |
The table names indexed by aliases. |
|---|---|---|
| throws | InvalidArgumentException | |
public function getTablesUsedInFrom(): array;
| public abstract array getUnions ( ) | ||
| return | array |
The "union" values. The format is:
|
|---|---|---|
public function getUnions(): array;
| public abstract array|Yiisoft\Db\Expression\ExpressionInterface|string|null getWhere ( ) | ||
| return | array|Yiisoft\Db\Expression\ExpressionInterface|string|null |
The "where" value. |
|---|---|---|
public function getWhere(): array|string|ExpressionInterface|null;
| public abstract Yiisoft\Db\Query\WithQuery[] getWithQueries ( ) | ||
| return | Yiisoft\Db\Query\WithQuery[] |
The "withQueries" value. |
|---|---|---|
public function getWithQueries(): array;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::groupBy()
Sets the GROUP BY part of the query.
See also addGroupBy().
| public abstract Yiisoft\Db\Query\QueryInterface groupBy ( array|Yiisoft\Db\Expression\ExpressionInterface|string $columns ) | ||
| $columns | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The columns to be grouped by. Columns can be specified in either a string (for example "id, name") or an array (such as ['id', 'name']). The method will automatically quote the column names unless a column has some parenthesis (which means the column has a DB expression). Note that if your group-by is an expression containing commas, you should always use an array to represent the group-by information. Otherwise, the method won't be able to correctly decide the group-by columns. {@see \Yiisoft\Db\Expression\ExpressionInterface} object can be passed to specify the |
public function groupBy(array|string|ExpressionInterface $columns): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::having()
Initially sets the HAVING part of the query.
See also:
| public abstract Yiisoft\Db\Query\QueryInterface having ( array|Yiisoft\Db\Expression\ExpressionInterface|string|null $condition, array $params = [] ) | ||
| $condition | array|Yiisoft\Db\Expression\ExpressionInterface|string|null |
The conditions to be put after |
| $params | array |
The parameters (name => value) to bind to the query. |
| throws | LogicException |
If |
|---|---|---|
public function having(array|ExpressionInterface|string|null $condition, array $params = []): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::indexBy()
Sets the {@see indexBy} property.
| public abstract Yiisoft\Db\Query\QueryInterface indexBy ( Closure|string|null $column ) | ||
| $column | Closure|string|null |
The name of the column by which the query results should be indexed by. This can also be callable (for example, anonymous function) that returns the index value based on the given data. The signature of the callable should be:
|
public function indexBy(string|Closure|null $column): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::innerJoin()
Appends an INNER JOIN part to the query.
| public abstract Yiisoft\Db\Query\QueryInterface innerJoin ( array|Yiisoft\Db\Expression\ExpressionInterface|string $table, array|Yiisoft\Db\Expression\ExpressionInterface|string $on = '', array $params = [] ) | ||
| $table | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The table to be joined. Use a string to represent the name of the table to be joined. The table name can contain a schema prefix (such as 'public.user') and/or table alias (such as 'user u'). The method will automatically quote the table name unless it has some parenthesis (which means the table is given as a sub-query or DB expression). Use an array to represent joining with a sub-query. The array must contain only one element. The value must be a {@see \Yiisoft\Db\Query\Query} object representing the sub-query while the corresponding key represents the alias for the sub-query. |
| $on | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The join condition that should appear in the ON part. Please refer to {@see \Yiisoft\Db\Query\join()} on how to specify this parameter. |
| $params | array |
The parameters (name => value) to bind to the query. |
public function innerJoin(
array|ExpressionInterface|string $table,
array|ExpressionInterface|string $on = '',
array $params = [],
): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::join()
Appends a JOIN part to the query.
The first parameter specifies what type of join it is.
| public abstract Yiisoft\Db\Query\QueryInterface join ( string $type, array|Yiisoft\Db\Expression\ExpressionInterface|string $table, array|Yiisoft\Db\Expression\ExpressionInterface|string $on = '', array $params = [] ) | ||
| $type | string |
The type of join, such as |
| $table | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The table to join. Use a string to represent the name of the table to join. The table name can contain a schema prefix (such as 'public.user') and/or table alias (e.g. 'user u'). The method will automatically quote the table name unless it has some parenthesis (which means the table is given as a sub-query or DB expression). Use an array to represent joining with a sub-query. The array must contain only one element. The value must be a {@see \Yiisoft\Db\Query\Query} object representing the sub-query while the corresponding key represents the alias for the sub-query. |
| $on | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The join condition that should appear in the ON part. Please refer to {@see \Yiisoft\Db\Query\where()} on how to specify this parameter. Keys and values of an associative array are treated as column names and will be quoted before being used in an SQL query. |
| $params | array |
The parameters (name => value) to bind to the query. |
public function join(
string $type,
array|ExpressionInterface|string $table,
array|ExpressionInterface|string $on = '',
array $params = [],
): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::leftJoin()
Appends a LEFT OUTER JOIN part to the query.
| public abstract Yiisoft\Db\Query\QueryInterface leftJoin ( array|Yiisoft\Db\Expression\ExpressionInterface|string $table, array|Yiisoft\Db\Expression\ExpressionInterface|string $on = '', array $params = [] ) | ||
| $table | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The table to join. Use a string to represent the name of the table to join. The table name can contain a schema prefix (such as 'public.user') and/or table alias (such as 'user u'). The method will automatically quote the table name unless it has some parenthesis (which means the table is given as a sub-query or DB expression). Use an array to represent joining with a sub-query. The array must contain only one element. The value must be a {@see \Yiisoft\Db\Query\Query} object representing the sub-query while the corresponding key represents the alias for the sub-query. |
| $on | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The join condition that should appear in the ON part. Please refer to {@see \Yiisoft\Db\Query\join()} on how to specify this parameter. |
| $params | array |
The parameters (name => value) to bind to the query. |
public function leftJoin(
array|ExpressionInterface|string $table,
array|ExpressionInterface|string $on = '',
array $params = [],
): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::limit()
Sets the LIMIT part of the query.
| public abstract Yiisoft\Db\Query\QueryInterface limit ( Yiisoft\Db\Expression\ExpressionInterface|integer|null $limit ) | ||
| $limit | Yiisoft\Db\Expression\ExpressionInterface|integer|null |
The limit. Use null or negative value to disable limit. |
public function limit(ExpressionInterface|int|null $limit): static;
Defined in: Yiisoft\Db\Query\QueryFunctionsInterface::max()
Returns the maximum of the specified column values.
| public abstract float|integer|string|null max ( string $sql ) | ||
| $sql | string |
The column name or expression. |
| return | float|integer|string|null |
The maximum of the specified column values. Note: Make sure you quote column names in the expression. |
|---|---|---|
| throws | Yiisoft\Db\Exception\InvalidConfigException | |
| throws | Throwable | |
| throws | Yiisoft\Db\Exception\Exception | |
public function max(string $sql): int|float|string|null;
Defined in: Yiisoft\Db\Query\QueryFunctionsInterface::min()
Returns the minimum of the specified column values.
| public abstract float|integer|string|null min ( string $sql ) | ||
| $sql | string |
The column name or expression. |
| return | float|integer|string|null |
The minimum of the specified column values. Note: Make sure you quote column names in the expression. |
|---|---|---|
| throws | Yiisoft\Db\Exception\InvalidConfigException | |
| throws | Throwable | |
| throws | Yiisoft\Db\Exception\Exception | |
public function min(string $sql): int|float|string|null;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::offset()
Sets the OFFSET part of the query.
| public abstract Yiisoft\Db\Query\QueryInterface offset ( Yiisoft\Db\Expression\ExpressionInterface|integer|null $offset ) | ||
| $offset | Yiisoft\Db\Expression\ExpressionInterface|integer|null |
$offset The offset. Use |
public function offset(ExpressionInterface|int|null $offset): static;
Executes the query and returns a single row of a result.
| public abstract array|object|null one ( ) | ||
| return | array|object|null |
The first row as an |
|---|---|---|
| throws | Yiisoft\Db\Exception\Exception | |
| throws | Yiisoft\Db\Exception\InvalidConfigException | |
| throws | Throwable | |
public function one(): array|object|null;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::orFilterHaving()
Adds HAVING condition to the existing one but ignores {@see Query::isEmpty()}.
The new condition and the existing one will be joined using the OR operator.
This method is similar to {@see \Yiisoft\Db\Query\orHaving()}. The main difference is that this method will remove {@see \Yiisoft\Db\Query\Query::isEmpty()}. As a result, this method is best suited for building query conditions based on filter values entered by users.
See also:
| public abstract Yiisoft\Db\Query\QueryInterface orFilterHaving ( array $condition ) | ||
| $condition | array |
The new |
| throws | Yiisoft\Db\Exception\NotSupportedException | |
|---|---|---|
public function orFilterHaving(array $condition): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::orFilterWhere()
Adds WHERE condition to the existing one but ignores {@see Query::isEmpty()}.
The new condition and the existing one will be joined using the 'OR' operator.
This method is similar to {@see \Yiisoft\Db\Query\orWhere()}. The main difference is that this method will remove {@see \Yiisoft\Db\Query\Query::isEmpty()}. As a result, this method is best suited for building query conditions based on filter values entered by users.
See also:
| public abstract Yiisoft\Db\Query\QueryInterface orFilterWhere ( array $condition ) | ||
| $condition | array |
The new |
| throws | Yiisoft\Db\Exception\NotSupportedException | |
|---|---|---|
public function orFilterWhere(array $condition): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::orHaving()
Adds HAVING condition to the existing one.
The new condition and the existing one will be joined using the OR operator.
See also:
| public abstract Yiisoft\Db\Query\QueryInterface orHaving ( array|Yiisoft\Db\Expression\ExpressionInterface|string $condition, array $params = [] ) | ||
| $condition | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The new |
| $params | array |
The parameters (name => value) to bind to the query. |
public function orHaving(array|string|ExpressionInterface $condition, array $params = []): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::orWhere()
Adds WHERE condition to the existing one.
The new condition and the existing one will be joined using the OR operator.
See also:
| public abstract Yiisoft\Db\Query\QueryInterface orWhere ( array|Yiisoft\Db\Expression\ExpressionInterface|string $condition, array $params = [] ) | ||
| $condition | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The new |
| $params | array |
The parameters (name => value) to bind to the query. |
public function orWhere(array|string|ExpressionInterface $condition, array $params = []): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::orderBy()
Sets the ORDER BY part of the query.
See also addOrderBy().
| public abstract Yiisoft\Db\Query\QueryInterface orderBy ( array|Yiisoft\Db\Expression\ExpressionInterface|string $columns ) | ||
| $columns | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The columns (and the directions) to be ordered by.
Columns can be specified in either a string (for example |
public function orderBy(array|string|ExpressionInterface $columns): static;
Sets the parameters to bind to the query.
See also addParams().
| public abstract Yiisoft\Db\Query\QueryInterface params ( array $params ) | ||
| $params | array |
List of query parameter values indexed by parameter placeholders.
For example, |
public function params(array $params): static;
Prepare for building SQL.
{@see \Yiisoft\Db\QueryBuilder\QueryBuilderInterface} uses this method when it starts to build SQL from a query object. You may override this method to do some final preparation work when converting a query into an SQL statement.
| public abstract self prepare ( Yiisoft\Db\QueryBuilder\QueryBuilderInterface $builder ) | ||
| $builder | Yiisoft\Db\QueryBuilder\QueryBuilderInterface |
The query builder. |
public function prepare(QueryBuilderInterface $builder): self;
Sets the callback, to be called on all rows of the query result before returning them.
For example:
$users = $db->createQuery()
->from('user')
->resultCallback(function (array $rows): array {
foreach ($rows as &$row) {
$row['name'] = strtoupper($row['name']);
}
return $rows;
})
->all();
| public abstract Yiisoft\Db\Query\QueryInterface resultCallback ( Closure|null $resultCallback ) | ||
| $resultCallback | Closure|null | |
public function resultCallback(?Closure $resultCallback): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::rightJoin()
Appends a RIGHT OUTER JOIN part to the query.
| public abstract Yiisoft\Db\Query\QueryInterface rightJoin ( array|Yiisoft\Db\Expression\ExpressionInterface|string $table, array|Yiisoft\Db\Expression\ExpressionInterface|string $on = '', array $params = [] ) | ||
| $table | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The table to be joined.
Use a string to represent the name of the table to be joined.
The table name can contain a schema prefix (such as |
| $on | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The join condition that should appear in the ON part. Please refer to {@see \Yiisoft\Db\Query\join()} on how to specify this parameter. |
| $params | array |
The parameters (name => value) to be bound to the query. |
public function rightJoin(
array|ExpressionInterface|string $table,
array|ExpressionInterface|string $on = '',
array $params = [],
): static;
Returns the query results as a scalar value.
The value returned will be the first column in the first row of the query results.
Do not use this method for boolean values as it returns false if the query result is empty.
| public abstract boolean|float|integer|string|null scalar ( ) | ||
| return | boolean|float|integer|string|null |
The value of the first column in the first row of the query result. False is returned if the query result is empty. |
|---|---|---|
| throws | Yiisoft\Db\Exception\Exception | |
| throws | Yiisoft\Db\Exception\InvalidConfigException | |
| throws | Throwable | |
public function scalar(): bool|int|string|float|null;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::select()
Sets the SELECT part of the query.
| public abstract Yiisoft\Db\Query\QueryInterface select ( array|Yiisoft\Db\Expression\ExpressionInterface|scalar $columns, string|null $option = null ) | ||
| $columns | array|Yiisoft\Db\Expression\ExpressionInterface|scalar |
The columns to be selected.
Columns can be specified in either a string (for example |
| $option | string|null |
More option that should be appended to the 'SELECT' keyword. For example, in MySQL, the option 'SQL_CALC_FOUND_ROWS' can be used. |
| Version | Description |
|---|---|
| 2.0.0 | `$columns` can be a scalar value or an array of scalar values. For example, `$query->select(1)` will be converted to `SELECT 1`. |
public function select(array|bool|float|int|string|ExpressionInterface $columns, ?string $option = null): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::selectOption()
It allows you to specify more options for the SELECT clause of an SQL statement.
| public abstract Yiisoft\Db\Query\QueryInterface selectOption ( string|null $value ) | ||
| $value | string|null |
More option that should be appended to the 'SELECT' keyword.
For example, in MySQL, the option |
public function selectOption(?string $value): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::setFor()
Overwrites the FOR part of the query.
See also:
| public abstract Yiisoft\Db\Query\QueryInterface setFor ( array|string|null $value ) | ||
| $value | array|string|null |
The value(s) to be set for the |
public function setFor(string|array|null $value): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::setHaving()
Overwrites the HAVING part of the query.
See also having().
| public abstract Yiisoft\Db\Query\QueryInterface setHaving ( array|Yiisoft\Db\Expression\ExpressionInterface|string|null $condition, array $params = [] ) | ||
| $condition | array|Yiisoft\Db\Expression\ExpressionInterface|string|null |
The conditions to be put after |
| $params | array |
The parameters (name => value) to bind to the query. |
public function setHaving(array|ExpressionInterface|string|null $condition, array $params = []): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::setJoins()
Specify the joins for a SELECT statement in a database query.
| public abstract Yiisoft\Db\Query\QueryInterface setJoins ( array $value ) | ||
| $value | array |
The joins to perform in the query. The format is the following:
|
public function setJoins(array $value): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::setUnions()
Specify queries for a SELECT statement that are combined with UNIONs.
| public abstract Yiisoft\Db\Query\QueryInterface setUnions ( array $value ) | ||
| $value | array |
The queries to union such as |
public function setUnions(array $value): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::setWhere()
Overwrites the WHERE part of the query.
See also where().
| public abstract Yiisoft\Db\Query\QueryInterface setWhere ( array|Yiisoft\Db\Expression\ExpressionInterface|string|null $condition, array $params = [] ) | ||
| $condition | array|Yiisoft\Db\Expression\ExpressionInterface|string|null |
The conditions to put in the |
| $params | array |
The parameters (name => value) to bind to the query. |
public function setWhere(array|string|ExpressionInterface|null $condition, array $params = []): static;
| public abstract boolean shouldEmulateExecution ( ) | ||
| return | boolean |
Whether to emulate query execution. |
|---|---|---|
public function shouldEmulateExecution(): bool;
Defined in: Yiisoft\Db\Query\QueryFunctionsInterface::sum()
Returns the sum of the specified column values.
| public abstract float|integer|string|null sum ( string $sql ) | ||
| $sql | string |
The column name or expression. |
| return | float|integer|string|null |
The sum of the specified column values. Note: Make sure you quote column names in the expression. |
|---|---|---|
| throws | Yiisoft\Db\Exception\InvalidConfigException | |
| throws | Throwable | |
| throws | Yiisoft\Db\Exception\Exception | |
public function sum(string $sql): int|float|string|null;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::union()
Appends an SQL statement using UNION operator.
| public abstract Yiisoft\Db\Query\QueryInterface union ( Yiisoft\Db\Query\QueryInterface|string $sql, boolean $all = false ) | ||
| $sql | Yiisoft\Db\Query\QueryInterface|string |
$sql The SQL statement to be appended using |
| $all | boolean |
|
public function union(QueryInterface|string $sql, bool $all = false): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::where()
Initially sets the WHERE part of the query.
The $condition specified as an array can be in one of the following two formats:
- key-value format:
['column1' => value1, 'column2' => value2, ...] - operator format:
[operator, operand1, operand2, ...]
A condition in key-value format represents the following SQL expression in general:
column1=value1 AND column2=value2 AND .... In case when a value is an array,
an IN expression will be generated. And if a value is null, IS NULL will be used in the generated
expression. Below are some examples:
['type' => 1, 'status' => 2]generates(type = 1) AND (status = 2).['id' => [1, 2, 3], 'status' => 2]generates(id IN (1, 2, 3)) AND (status = 2).['status' => null]generatesstatus IS NULL.
A condition in operator format generates the SQL expression according to the specified operator, which can be one of the following:
and: the operands should be concatenated together using
AND. For example,['and', 'id=1', 'id=2']will generateid=1 AND id=2. If an operand is an array, it will be converted into a string using the rules described here. For example,['and', 'type=1', ['or', 'id=1', 'id=2']]will generatetype=1 AND (id=1 OR id=2). The method will not do any quoting or escaping.or: similar to the
andoperator except that the operands are concatenated usingOR. For example,['or', ['type' => [7, 8, 9]], ['id' => [1, 2, 3]]]will generate(type IN (7, 8, 9) OR (id IN (1, 2, 3))).not: this will take only one operand and build the negation of it by prefixing the query string with
NOT. For example['not', ['attribute' => null]]will result in the conditionNOT (attribute IS NULL).between: operand 1 should be the column name, and operand 2 and 3 should be the starting and ending values of the range that the column is in. For example,
['between', 'id', 1, 10]will generateid BETWEEN 1 AND 10.not between: similar to
betweenexcept theBETWEENis replaced withNOT BETWEENin the generated condition.in: operand 1 should be a column or DB expression, and operand 2 be an array representing the range of the values that the column or DB expression should be in. For example,
['in', 'id', [1, 2, 3]]will generateid IN (1, 2, 3). The method will quote the column name and escape values in the range.To create a composite
INcondition you can use and array for the column name and value, where the values are indexed by the column name:['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']] ].You may also specify a sub-query that's used to get the values for the
IN-condition:['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' => 1])]not in: similar to the
inoperator except thatINis replaced withNOT INin the generated condition.like: operand 1 should be a column or DB expression, and operand 2 be a string or an array representing the values that the column or DB expression should be like. For example,
['like', 'name', 'tester']will generatename LIKE '%tester%'. When the value range is given as an array, manyLIKEpredicates will be generated and concatenated usingAND. For example,['like', 'name', ['test', 'sample']]will generatename LIKE '%test%' AND name LIKE '%sample%'. The method will quote the column name and escape special characters in the values. Sometimes, you may want to add the percentage characters to the matching value by yourself. You may supply a third operandfalseto do so. For example,['like', 'name', '%tester', false]will generatename LIKE '%tester'.or like: similar to the
likeoperator except thatORis used to concatenate theLIKEpredicates when operand 2 is an array.not like: similar to the
likeoperator except thatLIKEis replaced withNOT LIKEin the generated condition.or not like: similar to the
not likeoperator except thatORis used to concatenate theNOT LIKEpredicates.exists: operand 1 is a query object that used to build an
EXISTScondition. For example['exists', (new Query())->select('id')->from('users')->where(['active' => 1])]will result in the following SQL expression:EXISTS (SELECT "id" FROM "users" WHERE "active"=1).not exists: similar to the
existsoperator except thatEXISTSis replaced withNOT EXISTSin the generated condition.Additionally, you can specify arbitrary operators as follows: A condition of
['>=', 'id', 10]will result in the following SQL expression:id >= 10.
Note that this method will override any existing WHERE condition. You might want to use {@see \Yiisoft\Db\Query\andWhere()}
or {@see \Yiisoft\Db\Query\orWhere()} instead.
See also:
| public abstract Yiisoft\Db\Query\QueryInterface where ( array|Yiisoft\Db\Expression\ExpressionInterface|string|null $condition, array $params = [] ) | ||
| $condition | array|Yiisoft\Db\Expression\ExpressionInterface|string|null |
The conditions to put in the |
| $params | array |
The parameters (name => value) to bind to the query. |
| throws | LogicException |
If |
|---|---|---|
public function where(array|string|ExpressionInterface|null $condition, array $params = []): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::withQueries()
Specifies the WITH query clause for the query.
| public abstract Yiisoft\Db\Query\QueryInterface withQueries ( Yiisoft\Db\Query\WithQuery $queries ) | ||
| $queries | Yiisoft\Db\Query\WithQuery |
The |
public function withQueries(WithQuery ...$queries): static;
Defined in: Yiisoft\Db\Query\QueryPartsInterface::withQuery()
Set an SQL statement using WITH syntax.
| public abstract Yiisoft\Db\Query\QueryInterface withQuery ( Yiisoft\Db\Query\QueryInterface|string $query, Yiisoft\Db\Expression\ExpressionInterface|string $alias, boolean $recursive = false ) | ||
| $query | Yiisoft\Db\Query\QueryInterface|string |
The SQL statement. |
| $alias | Yiisoft\Db\Expression\ExpressionInterface|string |
The query alias in |
| $recursive | boolean |
Its |
public function withQuery(
QueryInterface|string $query,
ExpressionInterface|string $alias,
bool $recursive = false,
): static;
Returns a copy of the instance with enabled or disabled typecasting of values when retrieving records from DB.
| public abstract Yiisoft\Db\Query\QueryInterface withTypecasting ( boolean $typecasting = true ) | ||
| $typecasting | boolean | |
public function withTypecasting(bool $typecasting = true): static;
Signup or Login in order to comment.