やりたいこと
SQLのCASE文をCakephp3で使いたい。
SELECT
CASE WHEN name IS NULL THEN name
ELSE name_kana
END `user_name`
FROM
users;
Cakephp3やり方
use Cake\Database\Expression\IdentifierExpression;$userNameCase = $query->newExpr()
->addCase(
[$query->newExpr()->add(['Users.name IS NULL'])],
[new IdentifierExpression('Users.name'), new IdentifierExpression('Users.name_kana')],
['string', 'string']
);
$query->select([
'user_name' => $userNameCase
]);