Final Class Yiisoft\Db\Expression\Statement\CaseX
| Inheritance | Yiisoft\Db\Expression\Statement\CaseX |
|---|---|
| Implements | Yiisoft\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
| 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
| 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
The result to return if no conditions match in the CASE expression.
If not set, the CASE expression will not have an ELSE clause.
List of WHEN-THEN conditions and their corresponding results in the CASE expression.
Method Details
| 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
|
| $valueType | Yiisoft\Db\Schema\Column\ColumnInterface|string |
Optional data type of the |
| $args | mixed|Yiisoft\Db\Expression\Statement\WhenThen |
List of |
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;
}
Signup or Login in order to comment.