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
$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 mixed __construct ( Yiisoft\Db\Connection\ConnectionInterface $db )
$db Yiisoft\Db\Connection\ConnectionInterface

                public function __construct(protected ConnectionInterface $db) {}

            
addFor() public method

public Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query andFilterCompare ( string $column, string|null $value, string $defaultOperator '=' )
$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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 integer|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 integer|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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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|integer|null getLimit ( )

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

            
getOffset() public method

public Yiisoft\Db\Expression\ExpressionInterface|integer|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|null 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|null 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 integer|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 integer|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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 boolean|integer|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)
    ) {
        $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 Yiisoft\Db\Query\Query resultCallback ( Closure|null $resultCallback )
$resultCallback Closure|null

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

            
rightJoin() public method

public Yiisoft\Db\Query\Query 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 boolean|integer|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 Yiisoft\Db\Query\Query select ( array|boolean|float|integer|string|Yiisoft\Db\Expression\ExpressionInterface $columns, string|null $option null )
$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 Yiisoft\Db\Query\Query selectOption ( string|null $value )
$value string|null

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

            
setFor() public method

public Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query setJoins ( array $value )
$value array

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

            
setUnions() public method

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

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

            
setWhere() public method

public Yiisoft\Db\Query\Query 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 integer|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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query 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 Yiisoft\Db\Query\Query withTypecasting ( boolean $typecasting true )
$typecasting boolean

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