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 {@see \Yiisoft\Db\Query\createCommand()}, you can get a {@see \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 {@see \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 {@see 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
protected array|\Yiisoft\Db\Expression\ExpressionInterface|string|null $having null
$indexBy protected property
protected \Closure|string|null $indexBy null
$joins protected property
protected array $joins = []
$limit protected property
protected \Yiisoft\Db\Expression\ExpressionInterface|int|null $limit null
$offset protected property
protected \Yiisoft\Db\Expression\ExpressionInterface|int|null $offset null
$orderBy protected property
protected array $orderBy = []
$params protected property
protected array $params = []
$resultCallback protected property
protected ?\Closure $resultCallback null
$select protected property
protected array $select = []
$selectOption protected property
protected ?string $selectOption null
$union protected property
protected array $union = []
$where protected property
protected array|string|\Yiisoft\Db\Expression\ExpressionInterface|null $where null
$withQueries protected property

Method Details

Hide inherited methods

__construct() public method

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

                public function __construct(protected ConnectionInterface $db) {}

            
addFor() public method

public static addFor ( string|array|null $value )
$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 static addGroupBy ( array|string|Yiisoft\Db\Expression\ExpressionInterface $columns )
$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 static addOrderBy ( array|string|Yiisoft\Db\Expression\ExpressionInterface $columns )
$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 static addParams ( array $params )
$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 static addSelect ( array|boolean|float|integer|string|Yiisoft\Db\Expression\ExpressionInterface $columns )
$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 static addWithQuery ( Yiisoft\Db\Query\QueryInterface|string $query, Yiisoft\Db\Expression\ExpressionInterface|string $alias, boolean $recursive false )
$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 array all ( )

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

            
andFilterCompare() public method

public static andFilterCompare ( string $column, ?string $value, string $defaultOperator '=' )
$column string
$value ?string
$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 static andFilterHaving ( array $condition )
$condition array

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

            
andFilterWhere() public method

public static andFilterWhere ( array $condition )
$condition array

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

            
andHaving() public method

public static andHaving ( array|string|Yiisoft\Db\Expression\ExpressionInterface $condition, array $params = [] )
$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 static andWhere ( mixed $condition, array $params = [] )
$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 int|float|string|null average ( string $sql )
$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 Yiisoft\Db\Query\BatchQueryResultInterface batch ( integer $batchSize 100 )
$batchSize integer

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

            
column() public method

public array column ( )

                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 int|string count ( string $sql '*' )
$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 Yiisoft\Db\Command\CommandInterface createCommand ( )

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

            
distinct() public method

public static distinct ( boolean $value true )
$value boolean

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

            
each() public method

public Yiisoft\Db\Query\DataReaderInterface each ( )

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

            
emulateExecution() public method

public static emulateExecution ( boolean $value true )
$value boolean

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

            
exists() public method

public boolean exists ( )

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

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

            
filterWhere() public method

public static filterWhere ( array $condition )
$condition array

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

            
for() public method

public static for ( string|array|null $value )
$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 static from ( array|Yiisoft\Db\Expression\ExpressionInterface|string $tables )
$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 boolean getDistinct ( )

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

            
getFor() public method

public array getFor ( )

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

            
getFrom() public method

public array getFrom ( )

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

            
getGroupBy() public method

public array getGroupBy ( )

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

            
getHaving() public method

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

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

            
getIndexBy() public method

public \Closure|string|null getIndexBy ( )

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

            
getJoins() public method

public array getJoins ( )

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

            
getLimit() public method

public \Yiisoft\Db\Expression\ExpressionInterface|int|null getLimit ( )

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

            
getOffset() public method

public \Yiisoft\Db\Expression\ExpressionInterface|int|null getOffset ( )

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

            
getOrderBy() public method

public array getOrderBy ( )

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

            
getParams() public method

public array getParams ( )

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

            
getResultCallback() public method

public ?\Closure getResultCallback ( )

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

            
getSelect() public method

public array getSelect ( )

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

            
getSelectOption() public method

public ?string getSelectOption ( )

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

            
getTablesUsedInFrom() public method

public array getTablesUsedInFrom ( )

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

            
getUnions() public method

public array getUnions ( )

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

            
getWhere() public method

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

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

            
getWithQueries() public method

public array getWithQueries ( )

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

            
groupBy() public method

public static groupBy ( array|string|Yiisoft\Db\Expression\ExpressionInterface $columns )
$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 static having ( array|Yiisoft\Db\Expression\ExpressionInterface|string|null $condition, array $params = [] )
$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 array[]|object[] index ( array $rows )
$rows array

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

            
indexBy() public method

public static indexBy ( Closure|string|null $column )
$column Closure|string|null

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

            
innerJoin() public method

public static innerJoin ( array|Yiisoft\Db\Expression\ExpressionInterface|string $table, array|Yiisoft\Db\Expression\ExpressionInterface|string $on '', array $params = [] )
$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 static join ( string $type, array|Yiisoft\Db\Expression\ExpressionInterface|string $table, array|Yiisoft\Db\Expression\ExpressionInterface|string $on '', array $params = [] )
$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 static leftJoin ( array|Yiisoft\Db\Expression\ExpressionInterface|string $table, array|Yiisoft\Db\Expression\ExpressionInterface|string $on '', array $params = [] )
$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 static limit ( Yiisoft\Db\Expression\ExpressionInterface|integer|null $limit )
$limit Yiisoft\Db\Expression\ExpressionInterface|integer|null

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

            
max() public method

public int|float|string|null max ( string $sql )
$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 int|float|string|null min ( string $sql )
$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 static offset ( Yiisoft\Db\Expression\ExpressionInterface|integer|null $offset )
$offset Yiisoft\Db\Expression\ExpressionInterface|integer|null

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

            
one() public method

public array|object|null one ( )

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

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

            
orFilterWhere() public method

public static orFilterWhere ( array $condition )
$condition array

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

            
orHaving() public method

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

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

            
prepare() public method

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

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

            
queryScalar() protected method

Queries a scalar value by setting {@see select()} first.

Restores the value of select to make this query reusable.

protected bool|int|string|float|null queryScalar ( string|Yiisoft\Db\Expression\ExpressionInterface $selectExpression )
$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)
    ) {
        $query = clone $this;
        $query->select = [$selectExpression];
        $query->orderBy = [];
        $query->limit = null;
        $query->offset = null;
        return $query->createCommand()->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 static resultCallback ( ?\Closure $resultCallback )
$resultCallback ?\Closure

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

            
rightJoin() public method

public static rightJoin ( array|Yiisoft\Db\Expression\ExpressionInterface|string $table, array|Yiisoft\Db\Expression\ExpressionInterface|string $on '', array $params = [] )
$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 bool|int|string|float|null scalar ( )

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

            
select() public method

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

                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 static selectOption ( ?string $value )
$value ?string

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

            
setFor() public method

public static setFor ( array|string|null $value )
$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 static setHaving ( array|Yiisoft\Db\Expression\ExpressionInterface|string|null $condition, array $params = [] )
$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 static setJoins ( array $value )
$value array

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

            
setUnions() public method

public static setUnions ( array $value )
$value array

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

            
setWhere() public method

public static setWhere ( array|string|Yiisoft\Db\Expression\ExpressionInterface|null $condition, array $params = [] )
$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 boolean shouldEmulateExecution ( )

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

            
sum() public method

public int|float|string|null sum ( string $sql )
$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 static union ( Yiisoft\Db\Query\QueryInterface|string $sql, boolean $all false )
$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 static where ( array|string|Yiisoft\Db\Expression\ExpressionInterface|null $condition, array $params = [] )
$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 static withQueries ( Yiisoft\Db\Query\WithQuery $queries )
$queries Yiisoft\Db\Query\WithQuery

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

            
withQuery() public method

public static withQuery ( Yiisoft\Db\Query\QueryInterface|string $query, Yiisoft\Db\Expression\ExpressionInterface|string $alias, boolean $recursive false )
$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 static withTypecasting ( boolean $typecasting true )
$typecasting boolean

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