如何为 IF..ELSE 构建 Cakephp3 ORM 查询?
How to build Cakephp3 ORM query for IF.. ELSE?
我是 ORM 概念的新手。请将以下查询构建为 Cakphp3 ORM 格式。
select sum(case when tr_type = 'credit' then awards else -awards end) as balance
from user_orders where user_id = 12345;
尝试这样的事情:
$query = $this->UserOrders->find()->where(['user_id' => 12345]);
$awards = $query->newExpr()
->addCase(
[
$query->newExpr()->add(['tr_type' => 'credit'])
],
[
$query->newExpr()->add(['awards' => '-awards'])
]);
$query->select(['balance' => $query->func()->sum($awards)]);
更多信息:https://book.cakephp.org/3.0/en/orm/query-builder.html#using-sql-functions
可以看Class文件QueryExpresions.php,方法注释例子太多了。
我是 ORM 概念的新手。请将以下查询构建为 Cakphp3 ORM 格式。
select sum(case when tr_type = 'credit' then awards else -awards end) as balance
from user_orders where user_id = 12345;
尝试这样的事情:
$query = $this->UserOrders->find()->where(['user_id' => 12345]);
$awards = $query->newExpr()
->addCase(
[
$query->newExpr()->add(['tr_type' => 'credit'])
],
[
$query->newExpr()->add(['awards' => '-awards'])
]);
$query->select(['balance' => $query->func()->sum($awards)]);
更多信息:https://book.cakephp.org/3.0/en/orm/query-builder.html#using-sql-functions
可以看Class文件QueryExpresions.php,方法注释例子太多了。