我在用户模型中自定义了查找类型方法:
protected function _findStats($state, $query, $results = array()) {
if ($state === 'before') {
debug($query);
$query['fields'] = array('User.id', 'User.username', 'Click.clicks', 'Click.unique', 'Sale.sales_1', 'Sale.sales_2');
$query['joins'] = array(
array(
'table' => '(' . $this->__buildSubQueryClicks() . ')',
'alias' => 'Click',
'type' => 'LEFT',
'conditions' => array('User.id = Click.id')
),
array(
'table' => '(' . $this->__buildSubQuerySales() . ')',
'alias' => 'Sale',
'type' => 'LEFT',
'conditions' => array('User.id = Sale.id')
)
);
return $query;
}
return $results;
}
现在从其他控制器(命名ReportsController
)我想对这个查找的结果进行分页。我这样做是这样的:
public function admin_index() {
list($from, $to) = $this->_prepareCommonVariables();
$this->paginate = array('stats');
$this->User->recursive = 0;
$this->set('users', $this->paginate('User',
array(
'Click.created BETWEEN ? AND ?' => array($from, $to),
'Sale.sold BETWEEN ? AND ?' => array($from, $to)
),
array('User.username', 'Click.unique', 'Click.clicks', 'Sale.sales_1', 'Sale.sales_2')
));
}
问题是我只能按User.id
和User.username
字段(通常是用户字段)排序。即使我通过它的第三个参数指定了允许的字段Controller::paginate()
也不起作用。这些Paginator Helper
链接会生成正确的 URL(例如/admin/reports/index/sort:Click.clicks/direction:asc
),但我在 SQL 查询中看不到 ORDER BY 部分。我已经debug($query)
输入,例如当该_findStats()
字段为空时。 Click.clicks
$query['order']