我試圖使用自定義查詢的蛋糕,然後用分頁代碼如下結果:CakePHP的自定義查詢分頁
$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);
}
你是說'$ this-> paginate'代替'$ this-> validate'嗎? – pleasedontbelong