排序子查询不能在cakephp 2中工作

时间:2016-04-17 07:57:42

标签: cakephp cakephp-2.3

在控制器中我有

        $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)")); ?>

可点击排序链接并刷新页面。但排序不起作用。订单按虚拟列名称不会自动附加到查询。

1 个答案:

答案 0 :(得分:0)

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

按照:

  

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

尝试:

optionHtml += '<option value="' + attr + '">' + value[attr] + '</option>';