0 follower

Interface Yiisoft\Db\Query\QueryPartsInterface

Implemented byYiisoft\Db\Query\QueryInterface

This interface defines a set of methods to create and manipulate the different parts of a database query, such as the {@see addGroupBy()}, {@see addSelect()}, {@see addOrderBy()}, {@see andFilterCompare()}, etc.

{@see \Yiisoft\Db\Query\Query} uses these methods to build and manipulate SQL statements.

Public Methods

Hide inherited methods

Method Description Defined By
addFor() Adds more FOR parts to the existing ones. Yiisoft\Db\Query\QueryPartsInterface
addGroupBy() Adds more group-by columns to the existing ones. Yiisoft\Db\Query\QueryPartsInterface
addOrderBy() Adds more ORDER BY columns to the query. Yiisoft\Db\Query\QueryPartsInterface
addSelect() Add more columns to the SELECT part of the query. Yiisoft\Db\Query\QueryPartsInterface
addWithQuery() Prepends an SQL statement using WITH syntax. Yiisoft\Db\Query\QueryPartsInterface
andFilterCompare() Adds a filtering condition for a specific column and allow the user to choose a filter operator. Yiisoft\Db\Query\QueryPartsInterface
andFilterHaving() Adds HAVING condition to the existing one but ignores {@see Query::isEmpty()}. Yiisoft\Db\Query\QueryPartsInterface
andFilterWhere() Adds WHERE condition to the existing one but ignores {@see Query::isEmpty()}. Yiisoft\Db\Query\QueryPartsInterface
andHaving() Adds HAVING condition to the existing one. Yiisoft\Db\Query\QueryPartsInterface
andWhere() Adds WHERE condition to the existing one. Yiisoft\Db\Query\QueryPartsInterface
distinct() Sets the value indicating whether to SELECT DISTINCT or not. Yiisoft\Db\Query\QueryPartsInterface
filterHaving() Sets the HAVING part of the query but ignores {@see Query::isEmpty()}. Yiisoft\Db\Query\QueryPartsInterface
filterWhere() Sets the WHERE part of the query but ignores {@see Query::isEmpty()}. Yiisoft\Db\Query\QueryPartsInterface
for() Sets the FOR part of the query. Yiisoft\Db\Query\QueryPartsInterface
from() Sets the FROM part of the query. Yiisoft\Db\Query\QueryPartsInterface
groupBy() Sets the GROUP BY part of the query. Yiisoft\Db\Query\QueryPartsInterface
having() Initially sets the HAVING part of the query. Yiisoft\Db\Query\QueryPartsInterface
indexBy() Sets the {@see indexBy} property. Yiisoft\Db\Query\QueryPartsInterface
innerJoin() Appends an INNER JOIN part to the query. Yiisoft\Db\Query\QueryPartsInterface
join() Appends a JOIN part to the query. Yiisoft\Db\Query\QueryPartsInterface
leftJoin() Appends a LEFT OUTER JOIN part to the query. Yiisoft\Db\Query\QueryPartsInterface
limit() Sets the LIMIT part of the query. Yiisoft\Db\Query\QueryPartsInterface
offset() Sets the OFFSET part of the query. Yiisoft\Db\Query\QueryPartsInterface
orFilterHaving() Adds HAVING condition to the existing one but ignores {@see Query::isEmpty()}. Yiisoft\Db\Query\QueryPartsInterface
orFilterWhere() Adds WHERE condition to the existing one but ignores {@see Query::isEmpty()}. Yiisoft\Db\Query\QueryPartsInterface
orHaving() Adds HAVING condition to the existing one. Yiisoft\Db\Query\QueryPartsInterface
orWhere() Adds WHERE condition to the existing one. Yiisoft\Db\Query\QueryPartsInterface
orderBy() Sets the ORDER BY part of the query. Yiisoft\Db\Query\QueryPartsInterface
rightJoin() Appends a RIGHT OUTER JOIN part to the query. Yiisoft\Db\Query\QueryPartsInterface
select() Sets the SELECT part of the query. Yiisoft\Db\Query\QueryPartsInterface
selectOption() It allows you to specify more options for the SELECT clause of an SQL statement. Yiisoft\Db\Query\QueryPartsInterface
setFor() Overwrites the FOR part of the query. Yiisoft\Db\Query\QueryPartsInterface
setHaving() Overwrites the HAVING part of the query. Yiisoft\Db\Query\QueryPartsInterface
setJoins() Specify the joins for a SELECT statement in a database query. Yiisoft\Db\Query\QueryPartsInterface
setUnions() Specify queries for a SELECT statement that are combined with UNIONs. Yiisoft\Db\Query\QueryPartsInterface
setWhere() Overwrites the WHERE part of the query. Yiisoft\Db\Query\QueryPartsInterface
union() Appends an SQL statement using UNION operator. Yiisoft\Db\Query\QueryPartsInterface
where() Initially sets the WHERE part of the query. Yiisoft\Db\Query\QueryPartsInterface
withQueries() Specifies the WITH query clause for the query. Yiisoft\Db\Query\QueryPartsInterface
withQuery() Set an SQL statement using WITH syntax. Yiisoft\Db\Query\QueryPartsInterface

Method Details

Hide inherited methods

addFor() public abstract method

Adds more FOR parts to the existing ones.

See also:

public abstract Yiisoft\Db\Query\QueryPartsInterface addFor ( array|string|null $value )
$value array|string|null

The value(s) to be set for the FOR part. This can be either a string (for example, 'UPDATE') or a list of strings (such as ['SHARE OF {{t1}}', 'UPDATE OF {{t2}}']) specifying one or several values. null means adding nothing.

                public function addFor(string|array|null $value): static;

            
addGroupBy() public abstract method

Adds more group-by columns to the existing ones.

See also groupBy().

public abstract Yiisoft\Db\Query\QueryPartsInterface 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 GROUP BY part explicitly in plain SQL. {@see \Yiisoft\Db\Expression\ExpressionInterface} object can be passed as well.

                public function addGroupBy(array|string|ExpressionInterface $columns): static;

            
addOrderBy() public abstract method

Adds more ORDER BY columns to the query.

See also orderBy().

public abstract Yiisoft\Db\Query\QueryPartsInterface 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, ['id' => SORT_ASC, 'name' => SORT_DESC]). 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 order-by is an expression containing commas, you should always use an array to represent the order-by information. Otherwise, the method won't be able to correctly decide the order-by columns. Since {@see \Yiisoft\Db\Expression\ExpressionInterface} an object can be passed to specify the ORDER BY part explicitly in plain SQL.

                public function addOrderBy(array|string|ExpressionInterface $columns): static;

            
addSelect() public abstract method

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\QueryPartsInterface 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;

            
addWithQuery() public abstract method

Prepends an SQL statement using WITH syntax.

public abstract Yiisoft\Db\Query\QueryPartsInterface 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 UNION.

$alias Yiisoft\Db\Expression\ExpressionInterface|string

The query alias in WITH construction. To specify the alias in plain SQL, you may pass an instance of {@see \Yiisoft\Db\Expression\ExpressionInterface}.

$recursive boolean

Its true if using WITH RECURSIVE and false if using WITH.

                public function addWithQuery(
    QueryInterface|string $query,
    ExpressionInterface|string $alias,
    bool $recursive = false,
): static;

            
andFilterCompare() public abstract method

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 $defaultOperator is used.
public abstract Yiisoft\Db\Query\QueryPartsInterface 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 $value. Defaults to =, performing an exact match.

throws Yiisoft\Db\Exception\NotSupportedException

If this query doesn't support filtering.

                public function andFilterCompare(string $column, ?string $value, string $defaultOperator = '='): static;

            
andFilterHaving() public abstract method

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\QueryPartsInterface andFilterHaving ( array $condition )
$condition array

The new HAVING condition. Please refer to {@see \Yiisoft\Db\Query\having()} on how to specify this parameter.

throws Yiisoft\Db\Exception\NotSupportedException

If this query doesn't support filtering.

                public function andFilterHaving(array $condition): static;

            
andFilterWhere() public abstract method

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\QueryPartsInterface andFilterWhere ( array $condition )
$condition array

The new WHERE condition. Please refer to {@see \Yiisoft\Db\Query\where()} on how to specify this parameter.

throws Yiisoft\Db\Exception\NotSupportedException

If this query doesn't support filtering.

                public function andFilterWhere(array $condition): static;

            
andHaving() public abstract method

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\QueryPartsInterface 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;

            
andWhere() public abstract method

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\QueryPartsInterface andWhere ( array|Yiisoft\Db\Expression\ExpressionInterface|string $condition, array $params = [] )
$condition array|Yiisoft\Db\Expression\ExpressionInterface|string

The new WHERE 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 andWhere(array|ExpressionInterface|string $condition, array $params = []): static;

            
distinct() public abstract method

Sets the value indicating whether to SELECT DISTINCT or not.

public abstract Yiisoft\Db\Query\QueryPartsInterface distinct ( boolean $value true )
$value boolean

Whether to SELECT DISTINCT or not.

                public function distinct(bool $value = true): static;

            
filterHaving() public abstract method

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\QueryPartsInterface filterHaving ( array $condition )
$condition array

The conditions that should be in the HAVING part. See {@see \Yiisoft\Db\Query\having()} on how to specify this parameter.

throws Yiisoft\Db\Exception\NotSupportedException

If this query doesn't support filtering.

                public function filterHaving(array $condition): static;

            
filterWhere() public abstract method

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\QueryPartsInterface filterWhere ( array $condition )
$condition array

The conditions that should be in the WHERE part. {@see \Yiisoft\Db\Query\where()} On how to specify this parameter.

throws Yiisoft\Db\Exception\NotSupportedException

If this query doesn't support filtering.

                public function filterWhere(array $condition): static;

            
for() public abstract method

Sets the FOR part of the query.

See also:

public abstract Yiisoft\Db\Query\QueryPartsInterface for ( array|string|null $value )
$value array|string|null

The value(s) to be set for the FOR part. This can be either a string (for example, 'UPDATE') or a list of strings (such as ['SHARE OF {{t1}}', 'UPDATE OF {{t2}}']) specifying one or several values. null means no FOR part.

throws LogicException

If FOR was set previously.

                public function for(string|array|null $value): static;

            
from() public abstract method

Sets the FROM part of the query.

public abstract Yiisoft\Db\Query\QueryPartsInterface 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, 'user') or an array (such as ['user', 'profile']) specifying one or several table names. Table names can contain schema prefixes (such as 'public.user') and/or table aliases (such as 'user u'). The method will automatically quote the table names unless it has some parenthesis (which means the table is given as a sub-query or DB expression). When the tables are specified as an array, you may also use the array keys as the table aliases (if a table doesn't need alias, don't use a string key). Use a Query object to represent a sub-query. In this case, the corresponding array key will be used as the alias for the sub-query. To specify the FROM part in plain SQL, you may pass an instance of {@see \Yiisoft\Db\Expression\ExpressionInterface}. Here are some examples:

// SELECT * FROM  `user` `u`, `profile`;
$query = (new \Yiisoft\Db\Query\Query)->from(['u' => 'user', 'profile']);

// SELECT * FROM (SELECT * FROM `user` WHERE `active` = 1) `activeusers`;
$subQuery = (new \Yiisoft\Db\Query\Query)->from('user')->where(['active' => true])
$query = (new \Yiisoft\Db\Query\Query)->from(['activeusers' => $subQuery]);

// subQuery can also be a string with plain SQL wrapped in parentheses
// SELECT * FROM (SELECT * FROM `user` WHERE `active` = 1) `activeusers`;
$subQuery = "(SELECT * FROM `user` WHERE `active` = 1)";
$query = (new \Yiisoft\Db\Query\Query)->from(['activeusers' => $subQuery]);

                public function from(array|ExpressionInterface|string $tables): static;

            
groupBy() public abstract method

Sets the GROUP BY part of the query.

See also addGroupBy().

public abstract Yiisoft\Db\Query\QueryPartsInterface 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 GROUP BY part explicitly in plain SQL. {@see \Yiisoft\Db\Expression\ExpressionInterface} object can be passed as well.

                public function groupBy(array|string|ExpressionInterface $columns): static;

            
having() public abstract method

Initially sets the HAVING part of the query.

See also:

public abstract Yiisoft\Db\Query\QueryPartsInterface 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 HAVING. Please refer to {@see \Yiisoft\Db\Query\where()} on how to specify this parameter.

$params array

The parameters (name => value) to bind to the query.

throws LogicException

If having was set previously.

                public function having(array|ExpressionInterface|string|null $condition, array $params = []): static;

            
indexBy() public abstract method

Sets the {@see indexBy} property.

public abstract Yiisoft\Db\Query\QueryPartsInterface 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:

function (array|object $data): int|string
{
    // return the index value corresponding to $data
}

                public function indexBy(string|Closure|null $column): static;

            
innerJoin() public abstract method

Appends an INNER JOIN part to the query.

public abstract Yiisoft\Db\Query\QueryPartsInterface 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;

            
join() public abstract method

Appends a JOIN part to the query.

The first parameter specifies what type of join it is.

public abstract Yiisoft\Db\Query\QueryPartsInterface 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 INNER JOIN, LEFT JOIN.

$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;

            
leftJoin() public abstract method

Appends a LEFT OUTER JOIN part to the query.

public abstract Yiisoft\Db\Query\QueryPartsInterface 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;

            
limit() public abstract method

Sets the LIMIT part of the query.

public abstract Yiisoft\Db\Query\QueryPartsInterface 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;

            
offset() public abstract method

Sets the OFFSET part of the query.

public abstract Yiisoft\Db\Query\QueryPartsInterface offset ( Yiisoft\Db\Expression\ExpressionInterface|integer|null $offset )
$offset Yiisoft\Db\Expression\ExpressionInterface|integer|null

$offset The offset. Use null or negative value to disable offset.

                public function offset(ExpressionInterface|int|null $offset): static;

            
orFilterHaving() public abstract method

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\QueryPartsInterface orFilterHaving ( array $condition )
$condition array

The new HAVING condition. Please refer to {@see \Yiisoft\Db\Query\having()} on how to specify this parameter.

throws Yiisoft\Db\Exception\NotSupportedException

                public function orFilterHaving(array $condition): static;

            
orFilterWhere() public abstract method

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\QueryPartsInterface orFilterWhere ( array $condition )
$condition array

The new WHERE condition. Please refer to {@see \Yiisoft\Db\Query\where()} on how to specify this parameter.

throws Yiisoft\Db\Exception\NotSupportedException

                public function orFilterWhere(array $condition): static;

            
orHaving() public abstract method

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\QueryPartsInterface orHaving ( 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 bind to the query.

                public function orHaving(array|string|ExpressionInterface $condition, array $params = []): static;

            
orWhere() public abstract method

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\QueryPartsInterface orWhere ( array|Yiisoft\Db\Expression\ExpressionInterface|string $condition, array $params = [] )
$condition array|Yiisoft\Db\Expression\ExpressionInterface|string

The new WHERE condition. Please refer to {@see \Yiisoft\Db\Query\where()} on how to specify this parameter.

$params array

The parameters (name => value) to bind to the query.

                public function orWhere(array|string|ExpressionInterface $condition, array $params = []): static;

            
orderBy() public abstract method

Sets the ORDER BY part of the query.

See also addOrderBy().

public abstract Yiisoft\Db\Query\QueryPartsInterface 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 "id ASC, name DESC") or an array (such as ['id' => SORT_ASC, 'name' => SORT_DESC]). 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 order-by is an expression containing commas, you should always use an array to represent the order-by information. Otherwise, the method won't be able to correctly decide the order-by columns. Since {@see \Yiisoft\Db\Expression\ExpressionInterface} an object can be passed to specify the ORDER BY part explicitly in plain SQL.

                public function orderBy(array|string|ExpressionInterface $columns): static;

            
rightJoin() public abstract method

Appends a RIGHT OUTER JOIN part to the query.

public abstract Yiisoft\Db\Query\QueryPartsInterface 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 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 be bound to the query.

                public function rightJoin(
    array|ExpressionInterface|string $table,
    array|ExpressionInterface|string $on = '',
    array $params = [],
): static;

            
select() public abstract method

Sets the SELECT part of the query.

public abstract Yiisoft\Db\Query\QueryPartsInterface 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 id, name) or an array (such as ['id', 'name']). Columns can be prefixed with table names (such as user.id) and/or contain column aliases (for example user.id AS user_id). The method will automatically quote the column names unless a column has some parenthesis (which means the column has a DB expression). A DB expression may also be passed in form of an {@see \Yiisoft\Db\Expression\ExpressionInterface} object. Note that if you are selecting an expression like CONCAT(first_name, ' ', last_name), you should use an array to specify the columns. Otherwise, the expression may be incorrectly split into several parts. When the columns are specified as an array, you may also use array keys as the column aliases (if a column doesn't need alias, don't use a string key).

$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;

            
selectOption() public abstract method

It allows you to specify more options for the SELECT clause of an SQL statement.

public abstract Yiisoft\Db\Query\QueryPartsInterface selectOption ( string|null $value )
$value 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.

                public function selectOption(?string $value): static;

            
setFor() public abstract method

Overwrites the FOR part of the query.

See also:

public abstract Yiisoft\Db\Query\QueryPartsInterface setFor ( array|string|null $value )
$value array|string|null

The value(s) to be set for the FOR part. This can be either a string (for example, 'UPDATE') or a list of strings (such as ['SHARE OF {{t1}}', 'UPDATE OF {{t2}}']) specifying one or several values. null means no FOR part.

                public function setFor(string|array|null $value): static;

            
setHaving() public abstract method

Overwrites the HAVING part of the query.

See also having().

public abstract Yiisoft\Db\Query\QueryPartsInterface 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 HAVING.

$params array

The parameters (name => value) to bind to the query.

                public function setHaving(array|ExpressionInterface|string|null $condition, array $params = []): static;

            
setJoins() public abstract method

Specify the joins for a SELECT statement in a database query.

public abstract Yiisoft\Db\Query\QueryPartsInterface setJoins ( array $value )
$value array

The joins to perform in the query. The format is the following:

[
    ['INNER JOIN', 'table1', 'table1.id = table2.id'],
    ['LEFT JOIN', 'table3', 'table1.id = table3.id'],
]

                public function setJoins(array $value): static;

            
setUnions() public abstract method

Specify queries for a SELECT statement that are combined with UNIONs.

public abstract Yiisoft\Db\Query\QueryPartsInterface setUnions ( array $value )
$value array

The queries to union such as ['SELECT * FROM table1', 'SELECT * FROM table2'].

                public function setUnions(array $value): static;

            
setWhere() public abstract method

Overwrites the WHERE part of the query.

See also where().

public abstract Yiisoft\Db\Query\QueryPartsInterface 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 WHERE part.

$params array

The parameters (name => value) to bind to the query.

                public function setWhere(array|string|ExpressionInterface|null $condition, array $params = []): static;

            
union() public abstract method

Appends an SQL statement using UNION operator.

public abstract Yiisoft\Db\Query\QueryPartsInterface union ( Yiisoft\Db\Query\QueryInterface|string $sql, boolean $all false )
$sql Yiisoft\Db\Query\QueryInterface|string

$sql The SQL statement to be appended using UNION.

$all boolean

true if using UNION ALL and false if using UNION.

                public function union(QueryInterface|string $sql, bool $all = false): static;

            
where() public abstract method

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] generates status 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 generate id=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 generate type=1 AND (id=1 OR id=2). The method will not do any quoting or escaping.

  • or: similar to the and operator except that the operands are concatenated using OR. 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 condition NOT (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 generate id BETWEEN 1 AND 10.

  • not between: similar to between except the BETWEEN is replaced with NOT BETWEEN in 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 generate id IN (1, 2, 3). The method will quote the column name and escape values in the range.

    To create a composite IN condition 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 in operator except that IN is replaced with NOT IN in 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 generate name LIKE '%tester%'. When the value range is given as an array, many LIKE predicates will be generated and concatenated using AND. For example, ['like', 'name', ['test', 'sample']] will generate name 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 operand false to do so. For example, ['like', 'name', '%tester', false] will generate name LIKE '%tester'.

  • or like: similar to the like operator except that OR is used to concatenate the LIKE predicates when operand 2 is an array.

  • not like: similar to the like operator except that LIKE is replaced with NOT LIKE in the generated condition.

  • or not like: similar to the not like operator except that OR is used to concatenate the NOT LIKE predicates.

  • exists: operand 1 is a query object that used to build an EXISTS condition. 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 exists operator except that EXISTS is replaced with NOT EXISTS in 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\QueryPartsInterface 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 WHERE part.

$params array

The parameters (name => value) to bind to the query.

throws LogicException

If where was set previously.

                public function where(array|string|ExpressionInterface|null $condition, array $params = []): static;

            
withQueries() public abstract method

Specifies the WITH query clause for the query.

public abstract Yiisoft\Db\Query\QueryPartsInterface withQueries ( Yiisoft\Db\Query\WithQuery $queries )
$queries Yiisoft\Db\Query\WithQuery

The WITH queries to append to the query.

                public function withQueries(WithQuery ...$queries): static;

            
withQuery() public abstract method

Set an SQL statement using WITH syntax.

public abstract Yiisoft\Db\Query\QueryPartsInterface 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 WITH construction. To specify the alias in plain SQL, you may pass an instance of {@see \Yiisoft\Db\Expression\ExpressionInterface}.

$recursive boolean

Its true if using WITH RECURSIVE and false if using WITH.

                public function withQuery(
    QueryInterface|string $query,
    ExpressionInterface|string $alias,
    bool $recursive = false,
): static;