我正在尝试在Cake中使用自定义查询,然后使用以下代码对结果进行分页:
$query = $this->Petition->query($sql);
我试过了:
$petitions = $this->paginate($query);
它不起作用。我们有办法做到这一点吗?
好吧我还不够清楚:我需要在分页时使用从自定义查询中获取的变量数组,这样我就可以在视图中使用它进行分页。有这么简单的方法吗? 以下是我的代码:
function index() {
if ($this->Session->read('Auth.User.group_id') != 1) {
$commune_id = $this->Session->read('Auth.User.commune_id');
$commune_id = $this->Petition->Commune->findbyId($commune_id);
$commune_id = $this->Petition->Commune->find('all',array('conditions' => array('group' => $commune_id['Commune']['group'])));
$count = count($commune_id);
$i=1;
$sql = "SELECT * FROM `petitions` WHERE `commune_id` = ";
foreach($commune_id as $commune_ids){
if($i==1){
$sql .= $commune_ids['Commune']['id'];
}else{
$sql .= " OR `commune_id` = ".$commune_ids['Commune']['id'];
}
/*if($i != $count){
$this->paginate = array(
'or' => array(
array('Petition.commune_id LIKE' => $commune_ids['Commune']['id'] . ","),
//array('Petition.commune_id LIKE' => "," . $commune_ids['Commune']['id'] . ",")
),
'limit' => 10
);
}*/
$i++;
}
$query = $this->Petition->query($sql);
}
$this->Petition->recursive = 0;
$petitions = $this->paginate();
$this->set('petitions', $petitions);
}
答案 0 :(得分:0)
这不是分页的工作原理 你需要填写$ this->验证您的条件等 然后明白地使用$ this-> paginate()
见 http://book.cakephp.org/view/1232/Controller-Setup
还要注意有关自定义查询部分的章节,如果它真的是(!)必要的话。
答案 1 :(得分:0)
你真的需要阅读蛋糕书中的分页部分:
function index() { $conditions = array(); if ($this->Auth->user('group_id') != 1) { $commune_id = $this->Petition->Commune->findById($this->Auth->user('commune_id')); $conditions['Petition.id'] = $this->Petition->Commune->find('list',array( 'fields'=>array('id','id') 'conditions' => array('group' => $commune_id['Commune']['group']) )); } $this->Petition->recursive = 0; $petitions = $this->paginate('Petition',$conditions); $this->set('petitions', $petitions); }
类似的东西。