0 follower

Final Class Yiisoft\Db\Expression\Statement\CaseX

InheritanceYiisoft\Db\Expression\Statement\CaseX
ImplementsYiisoft\Db\Expression\ExpressionInterface

Represents a SQL CASE expression.

A CASE expression allows conditional logic in SQL queries, returning different values based on specified conditions. It can be used to implement complex logic directly in SQL statements.

Example usage:

$case = new CaseX(
    when1: new WhenThen(true, 'result1'),
    when2: new WhenThen(false, 'result2'),
    else: 'default result',
);

This will be generated into a SQL CASE expression like:

CASE
    WHEN TRUE THEN 'result1'
    WHEN FALSE THEN 'result2'
    ELSE 'default result'
END

Example with a specific case value:

$case = new CaseX(
     'column_name',
     when1: new WhenThen('one', 'result1'),
     when2: new WhenThen('two', 'result2'),
     else: 'default result',
);

This will be generated into a SQL CASE expression like:

CASE "column_name"
    WHEN 'one' THEN 'result1'
    WHEN 'two' THEN 'result2'
    ELSE 'default result'
END

Public Properties

Hide inherited properties

Property Type Description Defined By
$else mixed The result to return if no conditions match in the CASE expression. Yiisoft\Db\Expression\Statement\CaseX
$value mixed Yiisoft\Db\Expression\Statement\CaseX
$valueType string|Yiisoft\Db\Schema\Column\ColumnInterface Yiisoft\Db\Expression\Statement\CaseX
$whenThen Yiisoft\Db\Expression\Statement\WhenThen[] List of WHEN-THEN conditions and their corresponding results in the CASE expression. Yiisoft\Db\Expression\Statement\CaseX

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Db\Expression\Statement\CaseX
hasElse() Returns true if the CASE expression has an ELSE clause, false otherwise. Yiisoft\Db\Expression\Statement\CaseX

Property Details

Hide inherited properties

$else public property

The result to return if no conditions match in the CASE expression. If not set, the CASE expression will not have an ELSE clause.

public mixed $else null
$value public property
public mixed $value null
$valueType public property
$whenThen public property

List of WHEN-THEN conditions and their corresponding results in the CASE expression.

Method Details

Hide inherited methods

__construct() public method

public __construct( mixed $value null, Yiisoft\Db\Schema\Column\ColumnInterface|string $valueType '', mixed|Yiisoft\Db\Expression\Statement\WhenThen $args ): mixed
$value mixed

Comparison condition in the CASE expression:

  • string is treated as a table column name which will be quoted before usage in the SQL statement;
  • array is treated as a condition to check, see Yiisoft\Db\Query\QueryInterface::where();
  • other values will be converted to their string representation using \Yiisoft\Db\Expression\Statement\QueryBuilderInterface::buildValue(). If not provided, the CASE expression will be a WHEN-THEN structure without a specific case value.
$valueType Yiisoft\Db\Schema\Column\ColumnInterface|string

Optional data type of the CASE expression which can be used in some DBMS to specify the expected type (for example, in PostgreSQL).

$args mixed|Yiisoft\Db\Expression\Statement\WhenThen

List of WHEN-THEN conditions and their corresponding results represented as Yiisoft\Db\Expression\Statement\WhenThen instances or ELSE value in the CASE expression. String ELSE value will be quoted before usage in the SQL statement.

                public function __construct(
    public readonly mixed $value = null,
    public readonly string|ColumnInterface $valueType = '',
    mixed ...$args,
) {
    $whenThen = [];
    foreach ($args as $arg) {
        if ($arg instanceof WhenThen) {
            $whenThen[] = $arg;
        } elseif ($this->hasElse()) {
            throw new InvalidArgumentException('`CASE` expression can have only one `ELSE` value.');
        } else {
            $this->else = $arg;
        }
    }
    if (empty($whenThen)) {
        throw new InvalidArgumentException('`CASE` expression must have at least one `WHEN-THEN` clause.');
    }
    $this->whenThen = $whenThen;
}

            
hasElse() public method

Returns true if the CASE expression has an ELSE clause, false otherwise.

public hasElse( ): boolean

                public function hasElse(): bool
{
    return array_key_exists('else', get_object_vars($this));
}