使用子查询排序在cakephp 2中不起作用
在控制器中,我有使用子查询排序在cakephp 2中不起作用,cakephp,cakephp-2.3,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
$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)")); ?>
可单击排序链接并刷新页面。但这种分类方法不起作用。按虚拟列名排序不会自动附加到查询中。是否尝试将它们声明为虚拟字段 根据: 尝试:
$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'));