【问题标题】:sort with subquery not working in cakephp 2用子查询排序在 cakephp 2 中不起作用
【发布时间】:2016-04-17 07:57:41
【问题描述】:

在控制器中我有

        $this->paginate = array(
            'fields' => array(
                'User.id', 'User.username', 'User.fullname', 'User.role', 'User.user_remarks',
                '( SELECT IFNULL(SUM(B.profit), 0) FROM businesses B WHERE B.user_id = User.id ) +
 ( SELECT IFNULL(SUM(IFNULL(J.cost, 0)*IFNULL(J.duration, 0)), 0) FROM jobs J WHERE J.user_id = User.id ) +
 ( SELECT IFNULL(SUM(FI.income), 0) FROM fixed_incomes FI WHERE FI.user_id = User.id ) +
 ( SELECT IFNULL(SUM(OI.income), 0) FROM other_incomes OI WHERE OI.user_id = User.id ) AS total_income',
                '( SELECT IFNULL(SUM(FE.expense), 0) FROM fixed_expenses FE WHERE FE.user_id = User.id ) +
 ( SELECT IFNULL(SUM(E.cost), 0) FROM expenses E WHERE E.user_id = USER.id ) AS total_expense',
            ),
            'conditions' => $conditions,
        );

        $this->set('users', $this->Paginator->paginate('User'));

在视图中我有

<?php echo $this->Paginator->sort('total_income',__("Income (This month)")); ?>
<?php echo $this->Paginator->sort('total_expense',__("Spending (This month)")); ?>

排序链接可点击和页面刷新。但是排序不起作用。按虚拟列名排序,不会自动附加到查询中。

【问题讨论】:

    标签: cakephp cakephp-2.3


    【解决方案1】:

    您是否尝试将它们声明为虚拟字段?

    根据:

    http://book.cakephp.org/2.0/en/models/virtual-fields.html#virtual-fields-in-sql-queries

    尝试:

    $this->virtualFields['total_expense'] = 0;
    $this->virtualFields['total_income'] = 0;
    $this->paginate = array(
                'fields' => array(
                    'User.id', 'User.username', 'User.fullname', 'User.role', 'User.user_remarks',
                    '( SELECT IFNULL(SUM(B.profit), 0) FROM businesses B WHERE B.user_id = User.id ) +
     ( SELECT IFNULL(SUM(IFNULL(J.cost, 0)*IFNULL(J.duration, 0)), 0) FROM jobs J WHERE J.user_id = User.id ) +
     ( SELECT IFNULL(SUM(FI.income), 0) FROM fixed_incomes FI WHERE FI.user_id = User.id ) +
     ( SELECT IFNULL(SUM(OI.income), 0) FROM other_incomes OI WHERE OI.user_id = User.id ) AS User__total_income',
                    '( SELECT IFNULL(SUM(FE.expense), 0) FROM fixed_expenses FE WHERE FE.user_id = User.id ) +
     ( SELECT IFNULL(SUM(E.cost), 0) FROM expenses E WHERE E.user_id = USER.id ) AS User__total_expense',
                ),
                'conditions' => $conditions,
            );
    
            $this->set('users', $this->Paginator->paginate('User'));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      相关资源
      最近更新 更多