在我的CakePHP项目中,我需要使用自定义查询获取数据并对其进行分页。我成功使用了这样的自定义分页功能:
public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {
$recursive = -1;
$sql = "
SELECT Post.*, ....
FROM
(SELECT ....) //this is my problem
AS Post1
LEFT JOIN ...
LEFT JOIN ...
LEFT JOIN
LEFT JOIN
LEFT JOIN
GROUP BY Post1.id
ORDER BY ...
LIMIT " . (($page - 1) * $limit) . ', ' . $limit;
$results = $this->query($sql);
return $results;
}
我想要的结果是正确的,Paginator根据一个页面的限制值正确地划分行。 但排序功能:
echo $this->Paginator->sort('id', 'ID'); //don't works
如果我不使用自定义分页器功能,这可以正常工作,但我无法检索自定义查询等特定数据。
可以使用此设置示例:
$this->paginate = array(
'limit' => 10,
'fields' => array('Post.id', 'Post.title', 'Post.created'),
'conditions' => array('Post.title LIKE' => '%search_keyword%'),
'order' => array('Post.title' => 'asc', 'Post.id' => 'asc')
);
运行如下自定义查询:
SELECT Post.*, ....
FROM
(SELECT ../*complex query*/...) // <--this is my problem!!
AS Post1
LEFT JOIN ...
由于