【问题标题】:implement order by case with paginator in cakephp在 cakephp 中使用分页器实现逐个排序
【发布时间】:2014-02-05 06:21:20
【问题描述】:

我正在尝试在 ORDER BY 子句中使用 CASE 运算符。

以下将不起作用(忽略 ORDER BY 子句):

$this->Paginator->settings = array(
        'order' => array("CASE
                WHEN Transaction.created IS NOT NULL THEN Transaction.created
                WHEN Transaction.created IS NULL THEN Query.created
            END DESC")
    );

(虽然以下会很好:)

$this->Paginator->settings = array(
        'order' => array('Query.created' => 'desc'),
    );

在本机 CakePHP 2.4 查找方法中实现 MySQL CASE 的任何方式?

【问题讨论】:

    标签: php sql cakephp pagination


    【解决方案1】:

    尝试创建一个虚拟字段(假设您正在分页Transaction

    $this->Transaction->virtualFields['my_created'] = "CASE
                    WHEN Transaction.created IS NOT NULL THEN Transaction.created
                    WHEN Transaction.created IS NULL THEN Query.created
                END";
    
    $this->Paginator->settings = array(
            'order' => array('Transaction.my_created' => 'desc'),
        );
    

    【讨论】:

    • 像魅力一样工作。谢谢阿里利亚!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2016-06-12
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    相关资源
    最近更新 更多