0 follower

Class Yiisoft\Db\Query\Query

InheritanceYiisoft\Db\Query\Query
ImplementsYiisoft\Db\Query\QueryInterface

Represents a SELECT SQL statement in a way that's independent of DBMS.

Provides a set of methods to ease the specification of different clauses in a SELECT statement.

You can chain these methods together.

By calling createCommand(), you can get a Yiisoft\Db\Command\CommandInterface instance which can be further used to perform/execute the DB query in a database.

For example,

$query = new Query;

// compose the query
$query->select('id, name')->from('user')->limit(10);

// build and execute the query
$rows = $query->all();

// alternatively, you can create DB command and execute it
$command = $query->createCommand();

// $command->sql returns the actual SQL
$rows = $command->queryAll();

Query internally uses the Yiisoft\Db\QueryBuilder\AbstractQueryBuilder class to generate the SQL statement.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Db\Query\Query
addFor() Yiisoft\Db\Query\Query
addGroupBy() Yiisoft\Db\Query\Query
addOrderBy() Yiisoft\Db\Query\Query
addParams() Yiisoft\Db\Query\Query
addSelect() Yiisoft\Db\Query\Query
addWithQuery() Yiisoft\Db\Query\Query
all() Yiisoft\Db\Query\Query
andFilterCompare() Yiisoft\Db\Query\Query
andFilterHaving() Yiisoft\Db\Query\Query
andFilterWhere() Yiisoft\Db\Query\Query
andHaving() Yiisoft\Db\Query\Query
andWhere() Yiisoft\Db\Query\Query
average() Yiisoft\Db\Query\Query
batch() Yiisoft\Db\Query\Query
column() Yiisoft\Db\Query\Query
count() Yiisoft\Db\Query\Query
createCommand() Yiisoft\Db\Query\Query
distinct() Yiisoft\Db\Query\Query
each() Yiisoft\Db\Query\Query
emulateExecution() Yiisoft\Db\Query\Query
exists() Yiisoft\Db\Query\Query
filterHaving() Yiisoft\Db\Query\Query
filterWhere() Yiisoft\Db\Query\Query
for() Yiisoft\Db\Query\Query
from() Yiisoft\Db\Query\Query
getDistinct() Yiisoft\Db\Query\Query
getFor() Yiisoft\Db\Query\Query
getFrom() Yiisoft\Db\Query\Query
getGroupBy() Yiisoft\Db\Query\Query
getHaving() Yiisoft\Db\Query\Query
getIndexBy() Yiisoft\Db\Query\Query
getJoins() Yiisoft\Db\Query\Query
getLimit() Yiisoft\Db\Query\Query
getOffset() Yiisoft\Db\Query\Query
getOrderBy() Yiisoft\Db\Query\Query
getParams() Yiisoft\Db\Query\Query
getResultCallback() Yiisoft\Db\Query\Query
getSelect() Yiisoft\Db\Query\Query
getSelectOption() Yiisoft\Db\Query\Query
getTablesUsedInFrom() Yiisoft\Db\Query\Query
getUnions() Yiisoft\Db\Query\Query
getWhere() Yiisoft\Db\Query\Query
getWithQueries() Yiisoft\Db\Query\Query
groupBy() Yiisoft\Db\Query\Query
having() Yiisoft\Db\Query\Query
indexBy() Yiisoft\Db\Query\Query
innerJoin() Yiisoft\Db\Query\Query
join() Yiisoft\Db\Query\Query
leftJoin() Yiisoft\Db\Query\Query
limit() Yiisoft\Db\Query\Query
max() Yiisoft\Db\Query\Query
min() Yiisoft\Db\Query\Query
offset() Yiisoft\Db\Query\Query
one() Yiisoft\Db\Query\Query
orFilterHaving() Yiisoft\Db\Query\Query
orFilterWhere() Yiisoft\Db\Query\Query
orHaving() Yiisoft\Db\Query\Query
orWhere() Yiisoft\Db\Query\Query
orderBy() Yiisoft\Db\Query\Query
params() Yiisoft\Db\Query\Query
prepare() Yiisoft\Db\Query\Query
resultCallback() Yiisoft\Db\Query\Query
rightJoin() Yiisoft\Db\Query\Query
scalar() Yiisoft\Db\Query\Query
select() Yiisoft\Db\Query\Query
selectOption() Yiisoft\Db\Query\Query
setFor() Yiisoft\Db\Query\Query
setHaving() Yiisoft\Db\Query\Query
setJoins() Yiisoft\Db\Query\Query
setUnions() Yiisoft\Db\Query\Query
setWhere() Yiisoft\Db\Query\Query
shouldEmulateExecution() Yiisoft\Db\Query\Query
sum() Yiisoft\Db\Query\Query
union() Yiisoft\Db\Query\Query
where() Yiisoft\Db\Query\Query
withQueries() Yiisoft\Db\Query\Query
withQuery() Yiisoft\Db\Query\Query
withTypecasting() Yiisoft\Db\Query\Query

Protected Methods

Hide inherited methods

Method Description Defined By
index() Yiisoft\Db\Query\Query
queryScalar() Queries a scalar value by setting select() first. Yiisoft\Db\Query\Query

Property Details

Hide inherited properties

$db protected property
$distinct protected property
protected boolean $distinct false
$for protected property
protected array $for = []
$from protected property
protected array $from = []
$groupBy protected property
protected array $groupBy = []
$having protected property
$indexBy protected property
protected Closure|string|null $indexBy null
$joins protected property
protected array $joins = []
$limit protected property
$offset protected property
$orderBy protected property
protected array $orderBy = []
$params protected property
protected array $params = []
$resultCallback protected property
protected Closure|null $resultCallback null
$select protected property
protected array $select = []
$selectOption protected property
protected string|null $selectOption null
$union protected property
protected array $union = []
$where protected property
$withQueries protected property

Method Details

Hide inherited methods

__construct() public method

public __construct( Yiisoft\Db\Connection\ConnectionInterface $db ): mixed
$db Yiisoft\Db\Connection\ConnectionInterface

                public function __construct(protected ConnectionInterface $db) {}

            
addFor() public method

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

                public function addFor(string|array|null $value): static
{
    if ($value === null) {
        return $this;
    }
    if (is_array($value)) {
        $this->for = [...$this->for, ...$value];
        return $this;
    }
    $this->for[] = $value;
    return $this;
}

            
addGroupBy() public method

public addGroupBy( array|string|Yiisoft\Db\Expression\ExpressionInterface $columns ): Yiisoft\Db\Query\Query
$columns array|string|Yiisoft\Db\Expression\ExpressionInterface

                public function addGroupBy(array|string|ExpressionInterface $columns): static
{
    if ($columns instanceof ExpressionInterface) {
        $columns = [$columns];
    } elseif (!is_array($columns)) {
        /** @var string[] */
        $columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY);
    }
    if ($this->groupBy === []) {
        $this->groupBy = $columns;
    } else {
        $this->groupBy = array_merge($this->groupBy, $columns);
    }
    return $this;
}

            
addOrderBy() public method

public addOrderBy( array|string|Yiisoft\Db\Expression\ExpressionInterface $columns ): Yiisoft\Db\Query\Query
$columns array|string|Yiisoft\Db\Expression\ExpressionInterface

                public function addOrderBy(array|string|ExpressionInterface $columns): static
{
    $columns = $this->normalizeOrderBy($columns);
    if ($this->orderBy === []) {
        $this->orderBy = $columns;
    } else {
        $this->orderBy = array_merge($this->orderBy, $columns);
    }
    return $this;
}

            
addParams() public method

public addParams( array $params ): Yiisoft\Db\Query\Query
$params array

                public function addParams(array $params): static
{
    if (empty($params)) {
        return $this;
    }
    if (empty($this->params)) {
        $this->params = $params;
        return $this;
    }
    if (is_int(key($params))) {
        array_push($this->params, ...$params);
        return $this;
    }
    $this->params = [...$this->params, ...$params];
    return $this;
}

            
addSelect() public method

public addSelect( array|boolean|float|integer|string|Yiisoft\Db\Expression\ExpressionInterface $columns ): Yiisoft\Db\Query\Query
$columns array|boolean|float|integer|string|Yiisoft\Db\Expression\ExpressionInterface

                public function addSelect(array|bool|float|int|string|ExpressionInterface $columns): static
{
    if ($this->select === []) {
        return $this->select($columns);
    }
    $this->select = array_merge($this->select, $this->normalizeSelect($columns));
    return $this;
}

            
addWithQuery() public method

public addWithQuery( Yiisoft\Db\Query\QueryInterface|string $query, Yiisoft\Db\Expression\ExpressionInterface|string $alias, boolean $recursive false ): Yiisoft\Db\Query\Query
$query Yiisoft\Db\Query\QueryInterface|string
$alias Yiisoft\Db\Expression\ExpressionInterface|string
$recursive boolean

                public function addWithQuery(
    QueryInterface|string $query,
    ExpressionInterface|string $alias,
    bool $recursive = false,
): static {
    $this->withQueries[] = new WithQuery($query, $alias, $recursive);
    return $this;
}

            
all() public method

public all( ): array

                public function all(): array
{
    if ($this->emulateExecution === true) {
        return [];
    }
    return $this->index($this->createCommand()->queryAll());
}

            
andFilterCompare() public method

public andFilterCompare( string $column, string|null $value, string $defaultOperator '=' ): Yiisoft\Db\Query\Query
$column string
$value string|null
$defaultOperator string

                public function andFilterCompare(string $column, ?string $value, string $defaultOperator = '='): static
{
    $operator = $defaultOperator;
    if (preg_match('/^(<>|>=|>|<=|<|=)/', (string) $value, $matches)) {
        $operator = $matches[1];
        $value = substr((string) $value, strlen($operator));
    }
    return $this->andFilterWhere([$operator, $column, $value]);
}

            
andFilterHaving() public method

public andFilterHaving( array $condition ): Yiisoft\Db\Query\Query
$condition array

                public function andFilterHaving(array $condition): static
{
    $condition = $this->filterCondition($condition);
    if ($condition !== []) {
        $this->andHaving($condition);
    }
    return $this;
}

            
andFilterWhere() public method

public andFilterWhere( array $condition ): Yiisoft\Db\Query\Query
$condition array

                public function andFilterWhere(array $condition): static
{
    $condition = $this->filterCondition($condition);
    if ($condition !== []) {
        $this->andWhere($condition);
    }
    return $this;
}

            
andHaving() public method

public andHaving( array|string|Yiisoft\Db\Expression\ExpressionInterface $condition, array $params = [] ): Yiisoft\Db\Query\Query
$condition array|string|Yiisoft\Db\Expression\ExpressionInterface
$params array

                public function andHaving(array|string|ExpressionInterface $condition, array $params = []): static
{
    if ($this->having === null) {
        $this->having = $condition;
    } elseif (
        is_array($this->having)
        && isset($this->having[0])
        && strcasecmp((string) $this->having[0], 'and') === 0
    ) {
        $this->having[] = $condition;
    } else {
        $this->having = ['and', $this->having, $condition];
    }
    $this->addParams($params);
    return $this;
}

            
andWhere() public method

public andWhere( mixed $condition, array $params = [] ): Yiisoft\Db\Query\Query
$condition mixed
$params array

                public function andWhere($condition, array $params = []): static
{
    if ($this->where === null) {
        $this->where = $condition;
    } elseif (
        is_array($this->where)
        && isset($this->where[0])
        && strcasecmp((string) $this->where[0], 'and') === 0
    ) {
        $this->where[] = $condition;
    } else {
        $this->where = ['and', $this->where, $condition];
    }
    $this->addParams($params);
    return $this;
}

            
average() public method

public average( string $sql ): integer|float|string|null
$sql string

                public function average(string $sql): int|float|string|null
{
    return match ($this->emulateExecution) {
        true => null,
        false => is_numeric($avg = $this->queryScalar("AVG($sql)")) ? $avg : null,
    };
}

            
batch() public method

public batch( integer $batchSize 100 ): Yiisoft\Db\Query\BatchQueryResultInterface
$batchSize integer

                public function batch(int $batchSize = 100): BatchQueryResultInterface
{
    return $this->db
        ->createBatchQueryResult($this)
        ->batchSize($batchSize);
}

            
column() public method

public column( ): array

                public function column(): array
{
    if ($this->emulateExecution) {
        return [];
    }
    if ($this->indexBy === null) {
        return $this->createCommand()->queryColumn();
    }
    if (is_string($this->indexBy)) {
        if (count($this->select) === 1) {
            if (!str_contains($this->indexBy, '.') && count($tables = $this->getTablesUsedInFrom()) > 0) {
                $this->select[] = key($tables) . '.' . $this->indexBy;
            } else {
                $this->select[] = $this->indexBy;
            }
        }
        $rows = $this->createCommand()->queryAll();
        if (empty($rows)) {
            return [];
        }
        if (($dotPos = strpos($this->indexBy, '.')) === false) {
            $column = $this->indexBy;
        } else {
            $column = substr($this->indexBy, $dotPos + 1);
        }
        return array_column($rows, key(current($rows)), $column);
    }
    $rows = $this->createCommand()->queryAll();
    if (empty($rows)) {
        return [];
    }
    return array_combine(array_map($this->indexBy, $rows), array_column($rows, key(current($rows))));
}

            
count() public method

public count( string $sql '*' ): integer|string
$sql string

                public function count(string $sql = '*'): int|string
{
    /** @var int|string|null $count */
    $count = $this->queryScalar("COUNT($sql)");
    if ($count === null) {
        return 0;
    }
    /** @psalm-var non-negative-int|string */
    return $count <= PHP_INT_MAX ? (int) $count : $count;
}

            
createCommand() public method

public createCommand( ): Yiisoft\Db\Command\CommandInterface

                public function createCommand(): CommandInterface
{
    [$sql, $params] = $this->db->getQueryBuilder()->build($this);
    return $this->db->createCommand($sql, $params)->withPhpTypecasting($this->typecasting);
}

            
distinct() public method

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

                public function distinct(bool $value = true): static
{
    $this->distinct = $value;
    return $this;
}

            
each() public method

public each( ): Yiisoft\Db\Query\DataReaderInterface

                public function each(): DataReaderInterface
{
    return $this->createCommand()
        ->query()
        ->indexBy($this->indexBy)
        ->resultCallback($this->resultCallback !== null ? $this->callResultCallbackOnOne(...) : null);
}

            
emulateExecution() public method

public emulateExecution( boolean $value true ): Yiisoft\Db\Query\Query
$value boolean

                public function emulateExecution(bool $value = true): static
{
    $this->emulateExecution = $value;
    return $this;
}

            
exists() public method

public exists( ): boolean

                public function exists(): bool
{
    if ($this->emulateExecution) {
        return false;
    }
    $command = $this->createCommand();
    $params = $command->getParams();
    $command->setSql($this->db->getQueryBuilder()->selectExists($command->getSql()));
    $command->bindValues($params);
    return (bool) $command->queryScalar();
}

            
filterHaving() public method

public filterHaving( array $condition ): Yiisoft\Db\Query\Query
$condition array

                public function filterHaving(array $condition): static
{
    $condition = $this->filterCondition($condition);
    if ($condition !== []) {
        $this->having($condition);
    }
    return $this;
}

            
filterWhere() public method

public filterWhere( array $condition ): Yiisoft\Db\Query\Query
$condition array

                public function filterWhere(array $condition): static
{
    $condition = $this->filterCondition($condition);
    if ($condition !== []) {
        $this->where($condition);
    }
    return $this;
}

            
for() public method

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

                public function for(string|array|null $value): static
{
    if ($this->for !== []) {
        throw new LogicException('The `FOR` part was set earlier. Use the `setFor()` or `addFor()` method.');
    }
    return $this->setFor($value);
}

            
from() public method

public from( array|Yiisoft\Db\Expression\ExpressionInterface|string $tables ): Yiisoft\Db\Query\Query
$tables array|Yiisoft\Db\Expression\ExpressionInterface|string

                public function from(array|ExpressionInterface|string $tables): static
{
    $this->from = DbArrayHelper::normalizeExpressions($tables);
    return $this;
}

            
getDistinct() public method

public getDistinct( ): boolean

                public function getDistinct(): bool
{
    return $this->distinct;
}

            
getFor() public method

public getFor( ): array

                public function getFor(): array
{
    return $this->for;
}

            
getFrom() public method

public getFrom( ): array

                public function getFrom(): array
{
    return $this->from;
}

            
getGroupBy() public method

public getGroupBy( ): array

                public function getGroupBy(): array
{
    return $this->groupBy;
}

            
getHaving() public method

public getHaving( ): string|array|Yiisoft\Db\Expression\ExpressionInterface|null

                public function getHaving(): string|array|ExpressionInterface|null
{
    return $this->having;
}

            
getIndexBy() public method

public getIndexBy( ): Closure|string|null

                public function getIndexBy(): Closure|string|null
{
    return $this->indexBy;
}

            
getJoins() public method

public getJoins( ): array

                public function getJoins(): array
{
    return $this->joins;
}

            
getLimit() public method

public getLimit( ): Yiisoft\Db\Expression\ExpressionInterface|integer|null

                public function getLimit(): ExpressionInterface|int|null
{
    return $this->limit;
}

            
getOffset() public method

public getOffset( ): Yiisoft\Db\Expression\ExpressionInterface|integer|null

                public function getOffset(): ExpressionInterface|int|null
{
    return $this->offset;
}

            
getOrderBy() public method

public getOrderBy( ): array

                public function getOrderBy(): array
{
    return $this->orderBy;
}

            
getParams() public method

public getParams( ): array

                public function getParams(): array
{
    return $this->params;
}

            
getResultCallback() public method

public getResultCallback( ): Closure|null

                public function getResultCallback(): ?Closure
{
    return $this->resultCallback;
}

            
getSelect() public method

public getSelect( ): array

                public function getSelect(): array
{
    return $this->select;
}

            
getSelectOption() public method

public getSelectOption( ): string|null

                public function getSelectOption(): ?string
{
    return $this->selectOption;
}

            
getTablesUsedInFrom() public method

public getTablesUsedInFrom( ): array

                public function getTablesUsedInFrom(): array
{
    return $this->db->getQuoter()->cleanUpTableNames($this->from);
}

            
getUnions() public method

public getUnions( ): array

                public function getUnions(): array
{
    return $this->union;
}

            
getWhere() public method

public getWhere( ): array|string|Yiisoft\Db\Expression\ExpressionInterface|null

                public function getWhere(): array|string|ExpressionInterface|null
{
    return $this->where;
}

            
getWithQueries() public method

public getWithQueries( ): array

                public function getWithQueries(): array
{
    return $this->withQueries;
}

            
groupBy() public method

public groupBy( array|string|Yiisoft\Db\Expression\ExpressionInterface $columns ): Yiisoft\Db\Query\Query
$columns array|string|Yiisoft\Db\Expression\ExpressionInterface

                public function groupBy(array|string|ExpressionInterface $columns): static
{
    $this->groupBy = DbArrayHelper::normalizeExpressions($columns);
    return $this;
}

            
having() public method

public having( array|Yiisoft\Db\Expression\ExpressionInterface|string|null $condition, array $params = [] ): Yiisoft\Db\Query\Query
$condition array|Yiisoft\Db\Expression\ExpressionInterface|string|null
$params array

                public function having(array|ExpressionInterface|string|null $condition, array $params = []): static
{
    if ($this->having === null) {
        $this->having = $condition;
    } else {
        throw new LogicException('The `having` condition was set earlier. Use the `setHaving()`, `andHaving()` or `orHaving()` method.');
    }
    $this->addParams($params);
    return $this;
}

            
index() protected method

protected index( array $rows ): array[]|object[]
$rows array

                protected function index(array $rows): array
{
    return DbArrayHelper::index($rows, $this->indexBy, $this->resultCallback);
}

            
indexBy() public method

public indexBy( Closure|string|null $column ): Yiisoft\Db\Query\Query
$column Closure|string|null

                public function indexBy(Closure|string|null $column): static
{
    $this->indexBy = $column;
    return $this;
}

            
innerJoin() public method

public innerJoin( array|Yiisoft\Db\Expression\ExpressionInterface|string $table, array|Yiisoft\Db\Expression\ExpressionInterface|string $on '', array $params = [] ): Yiisoft\Db\Query\Query
$table array|Yiisoft\Db\Expression\ExpressionInterface|string
$on array|Yiisoft\Db\Expression\ExpressionInterface|string
$params array

                public function innerJoin(
    array|ExpressionInterface|string $table,
    array|ExpressionInterface|string $on = '',
    array $params = [],
): static {
    $this->joins[] = ['INNER JOIN', $table, $on];
    return $this->addParams($params);
}

            
join() public method

public join( string $type, array|Yiisoft\Db\Expression\ExpressionInterface|string $table, array|Yiisoft\Db\Expression\ExpressionInterface|string $on '', array $params = [] ): Yiisoft\Db\Query\Query
$type string
$table array|Yiisoft\Db\Expression\ExpressionInterface|string
$on array|Yiisoft\Db\Expression\ExpressionInterface|string
$params array

                public function join(
    string $type,
    array|ExpressionInterface|string $table,
    array|ExpressionInterface|string $on = '',
    array $params = [],
): static {
    $this->joins[] = [$type, $table, $on];
    return $this->addParams($params);
}

            
leftJoin() public method

public leftJoin( array|Yiisoft\Db\Expression\ExpressionInterface|string $table, array|Yiisoft\Db\Expression\ExpressionInterface|string $on '', array $params = [] ): Yiisoft\Db\Query\Query
$table array|Yiisoft\Db\Expression\ExpressionInterface|string
$on array|Yiisoft\Db\Expression\ExpressionInterface|string
$params array

                public function leftJoin(
    array|ExpressionInterface|string $table,
    array|ExpressionInterface|string $on = '',
    array $params = [],
): static {
    $this->joins[] = ['LEFT JOIN', $table, $on];
    return $this->addParams($params);
}

            
limit() public method

public limit( Yiisoft\Db\Expression\ExpressionInterface|integer|null $limit ): Yiisoft\Db\Query\Query
$limit Yiisoft\Db\Expression\ExpressionInterface|integer|null

                public function limit(ExpressionInterface|int|null $limit): static
{
    $this->limit = $limit;
    return $this;
}

            
max() public method

public max( string $sql ): integer|float|string|null
$sql string

                public function max(string $sql): int|float|string|null
{
    $max = $this->queryScalar("MAX($sql)");
    return is_numeric($max) ? $max : null;
}

            
min() public method

public min( string $sql ): integer|float|string|null
$sql string

                public function min(string $sql): int|float|string|null
{
    $min = $this->queryScalar("MIN($sql)");
    return is_numeric($min) ? $min : null;
}

            
offset() public method

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

                public function offset(ExpressionInterface|int|null $offset): static
{
    $this->offset = $offset;
    return $this;
}

            
one() public method

public one( ): array|object|null

                public function one(): array|object|null
{
    if ($this->emulateExecution) {
        return null;
    }
    $row = $this->createCommand()->queryOne();
    if ($this->resultCallback === null || $row === null) {
        return $row;
    }
    return $this->callResultCallbackOnOne($row);
}

            
orFilterHaving() public method

public orFilterHaving( array $condition ): Yiisoft\Db\Query\Query
$condition array

                public function orFilterHaving(array $condition): static
{
    $condition = $this->filterCondition($condition);
    if ($condition !== []) {
        $this->orHaving($condition);
    }
    return $this;
}

            
orFilterWhere() public method

public orFilterWhere( array $condition ): Yiisoft\Db\Query\Query
$condition array

                public function orFilterWhere(array $condition): static
{
    $condition = $this->filterCondition($condition);
    if ($condition !== []) {
        $this->orWhere($condition);
    }
    return $this;
}

            
orHaving() public method

public orHaving( array|string|Yiisoft\Db\Expression\ExpressionInterface $condition, array $params = [] ): Yiisoft\Db\Query\Query
$condition array|string|Yiisoft\Db\Expression\ExpressionInterface
$params array

                public function orHaving(array|string|ExpressionInterface $condition, array $params = []): static
{
    if ($this->having === null) {
        $this->having = $condition;
    } else {
        $this->having = ['or', $this->having, $condition];
    }
    $this->addParams($params);
    return $this;
}

            
orWhere() public method

public orWhere( array|string|Yiisoft\Db\Expression\ExpressionInterface $condition, array $params = [] ): Yiisoft\Db\Query\Query
$condition array|string|Yiisoft\Db\Expression\ExpressionInterface
$params array

                public function orWhere(array|string|ExpressionInterface $condition, array $params = []): static
{
    if ($this->where === null) {
        $this->where = $condition;
    } else {
        $this->where = ['or', $this->where, $condition];
    }
    $this->addParams($params);
    return $this;
}

            
orderBy() public method

public orderBy( array|string|Yiisoft\Db\Expression\ExpressionInterface $columns ): Yiisoft\Db\Query\Query
$columns array|string|Yiisoft\Db\Expression\ExpressionInterface

                public function orderBy(array|string|ExpressionInterface $columns): static
{
    $this->orderBy = $this->normalizeOrderBy($columns);
    return $this;
}

            
params() public method

public params( array $params ): Yiisoft\Db\Query\Query
$params array

                public function params(array $params): static
{
    $this->params = $params;
    return $this;
}

            
prepare() public method

public prepare( Yiisoft\Db\QueryBuilder\QueryBuilderInterface $builder ): Yiisoft\Db\Query\QueryInterface
$builder Yiisoft\Db\QueryBuilder\QueryBuilderInterface

                public function prepare(QueryBuilderInterface $builder): QueryInterface
{
    return $this;
}

            
queryScalar() protected method

Queries a scalar value by setting select() first.

Restores the value of select to make this query reusable.

protected queryScalar( string|Yiisoft\Db\Expression\ExpressionInterface $selectExpression ): boolean|integer|string|float|null
$selectExpression string|Yiisoft\Db\Expression\ExpressionInterface
throws Yiisoft\Db\Exception\Exception
throws InvalidArgumentException
throws Yiisoft\Db\Exception\InvalidConfigException
throws Yiisoft\Db\Exception\NotSupportedException
throws Throwable

                protected function queryScalar(string|ExpressionInterface $selectExpression): bool|int|string|float|null
{
    if ($this->emulateExecution) {
        return null;
    }
    if (
        !$this->distinct
        && empty($this->groupBy)
        && empty($this->having)
        && empty($this->union)
    ) {
        $select = $this->select;
        $order = $this->orderBy;
        $limit = $this->limit;
        $offset = $this->offset;
        $this->select = [$selectExpression];
        $this->orderBy = [];
        $this->limit = null;
        $this->offset = null;
        $command = $this->createCommand();
        $this->select = $select;
        $this->orderBy = $order;
        $this->limit = $limit;
        $this->offset = $offset;
        return $command->queryScalar();
    }
    $query = (new self($this->db))->select($selectExpression)->from(['c' => $this]);
    [$sql, $params] = $this->db->getQueryBuilder()->build($query);
    $command = $this->db->createCommand($sql, $params);
    return $command->queryScalar();
}

            
resultCallback() public method

public resultCallback( Closure|null $resultCallback ): Yiisoft\Db\Query\Query
$resultCallback Closure|null

                public function resultCallback(?Closure $resultCallback): static
{
    $this->resultCallback = $resultCallback;
    return $this;
}

            
rightJoin() public method

public rightJoin( array|Yiisoft\Db\Expression\ExpressionInterface|string $table, array|Yiisoft\Db\Expression\ExpressionInterface|string $on '', array $params = [] ): Yiisoft\Db\Query\Query
$table array|Yiisoft\Db\Expression\ExpressionInterface|string
$on array|Yiisoft\Db\Expression\ExpressionInterface|string
$params array

                public function rightJoin(
    array|ExpressionInterface|string $table,
    array|ExpressionInterface|string $on = '',
    array $params = [],
): static {
    $this->joins[] = ['RIGHT JOIN', $table, $on];
    return $this->addParams($params);
}

            
scalar() public method

public scalar( ): boolean|integer|string|float|null

                public function scalar(): bool|int|string|float|null
{
    return match ($this->emulateExecution) {
        true => null,
        false => $this->createCommand()->queryScalar(),
    };
}

            
select() public method

public select( array|boolean|float|integer|string|Yiisoft\Db\Expression\ExpressionInterface $columns, string|null $option null ): Yiisoft\Db\Query\Query
$columns array|boolean|float|integer|string|Yiisoft\Db\Expression\ExpressionInterface
$option string|null

                public function select(array|bool|float|int|string|ExpressionInterface $columns, ?string $option = null): static
{
    $this->select = $this->normalizeSelect($columns);
    $this->selectOption = $option;
    return $this;
}

            
selectOption() public method

public selectOption( string|null $value ): Yiisoft\Db\Query\Query
$value string|null

                public function selectOption(?string $value): static
{
    $this->selectOption = $value;
    return $this;
}

            
setFor() public method

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

                public function setFor(array|string|null $value): static
{
    if ($value === null) {
        $this->for = [];
        return $this;
    }
    $this->for = (array) $value;
    return $this;
}

            
setHaving() public method

public setHaving( array|Yiisoft\Db\Expression\ExpressionInterface|string|null $condition, array $params = [] ): Yiisoft\Db\Query\Query
$condition array|Yiisoft\Db\Expression\ExpressionInterface|string|null
$params array

                public function setHaving(array|ExpressionInterface|string|null $condition, array $params = []): static
{
    $this->having = $condition;
    $this->addParams($params);
    return $this;
}

            
setJoins() public method

public setJoins( array $value ): Yiisoft\Db\Query\Query
$value array

                public function setJoins(array $value): static
{
    $this->joins = $value;
    return $this;
}

            
setUnions() public method

public setUnions( array $value ): Yiisoft\Db\Query\Query
$value array

                public function setUnions(array $value): static
{
    $this->union = $value;
    return $this;
}

            
setWhere() public method

public setWhere( array|string|Yiisoft\Db\Expression\ExpressionInterface|null $condition, array $params = [] ): Yiisoft\Db\Query\Query
$condition array|string|Yiisoft\Db\Expression\ExpressionInterface|null
$params array

                public function setWhere(array|string|ExpressionInterface|null $condition, array $params = []): static
{
    $this->where = $condition;
    $this->addParams($params);
    return $this;
}

            
shouldEmulateExecution() public method

public shouldEmulateExecution( ): boolean

                public function shouldEmulateExecution(): bool
{
    return $this->emulateExecution;
}

            
sum() public method

public sum( string $sql ): integer|float|string|null
$sql string

                public function sum(string $sql): int|float|string|null
{
    return match ($this->emulateExecution) {
        true => null,
        false => is_numeric($sum = $this->queryScalar("SUM($sql)")) ? $sum : null,
    };
}

            
union() public method

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

                public function union(QueryInterface|string $sql, bool $all = false): static
{
    $this->union[] = ['query' => $sql, 'all' => $all];
    return $this;
}

            
where() public method

public where( array|string|Yiisoft\Db\Expression\ExpressionInterface|null $condition, array $params = [] ): Yiisoft\Db\Query\Query
$condition array|string|Yiisoft\Db\Expression\ExpressionInterface|null
$params array

                public function where(array|string|ExpressionInterface|null $condition, array $params = []): static
{
    if ($this->where !== null) {
        throw new LogicException(
            'The `where` condition was set earlier. Use the `setWhere()`, `andWhere()` or `orWhere()` method.',
        );
    }
    $this->where = $condition;
    $this->addParams($params);
    return $this;
}

            
withQueries() public method

public withQueries( Yiisoft\Db\Query\WithQuery $queries ): Yiisoft\Db\Query\Query
$queries Yiisoft\Db\Query\WithQuery

                public function withQueries(WithQuery ...$queries): static
{
    $this->withQueries = $queries;
    return $this;
}

            
withQuery() public method

public withQuery( Yiisoft\Db\Query\QueryInterface|string $query, Yiisoft\Db\Expression\ExpressionInterface|string $alias, boolean $recursive false ): Yiisoft\Db\Query\Query
$query Yiisoft\Db\Query\QueryInterface|string
$alias Yiisoft\Db\Expression\ExpressionInterface|string
$recursive boolean

                public function withQuery(
    QueryInterface|string $query,
    ExpressionInterface|string $alias,
    bool $recursive = false,
): static {
    $this->withQueries = [new WithQuery($query, $alias, $recursive)];
    return $this;
}

            
withTypecasting() public method

public withTypecasting( boolean $typecasting true ): Yiisoft\Db\Query\Query
$typecasting boolean

                public function withTypecasting(bool $typecasting = true): static
{
    $new = clone $this;
    $new->typecasting = $typecasting;
    return $new;
}