CakePHP 3.x案例说明

问题描述 投票:0回答:1

我试图在CakePHP 3.x应用程序中按MySQL语句的顺序使用CASE语句。简单选择如下:

$articles = $this->Articles->find()
    ->where($conditions)   
    ->order(function ($exp, $q) {
        return $exp->addCase(
            [
                $q->newExpr()->gt('Articles.modified', (new Time())->subDays(365)) // article has been updated in the last x days
                    ],
            ['priority'], # values matching conditions
            ['string'] # type of each value
        );
    })
->limit(15)
->all();

将生成以下SQL:

SELECT `Articles`.`id` AS `Articles__id`, .... 
FROM `articles` `Articles` 
WHERE (`publish` < :c0 AND `Articles`.`publish` > :c1) 
ORDER BY CASE WHEN `Articles`.`modified` > :c2 THEN :param3 END LIMIT 15

case语句不正确,因为它缺少应该在'END'之后出现的DESC顺序-请参见此小提琴:

http://sqlfiddle.com/#!9/8df161/5

我不确定这是否是CakePHP处理CASE的限制?

此外,我还需要在case语句后再按第二个顺序才能按'发布'的顺序进行排序。

cakephp sql-order-by query-builder cakephp-3.x
1个回答
0
投票

传递给Query::order()的表达式必须生成ORDER BY子句所需的所有内容,包括direction关键字。

如果您使用的表达式不支持该表达式,则可以使用Query::oderAsc()Query::oderAsc(),它们将相应地附加相应的direction关键字。

Query::oderDesc()

另请参见

  • Query::oderDesc()
© www.soinside.com 2019 - 2024. All rights reserved.