2011-08-18 175 views
0

我試圖使用自定義查詢的蛋糕,然後用分頁代碼如下結果: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); 
    } 

回答

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); 
} 

類似的東西。

0

這不是分頁是如何工作的 您需要填寫$這個 - >您的條件等 驗證,然後使用$這 - > PAGINATE()顯然

http://book.cakephp.org/view/1232/Controller-Setup

還注意到有關自定義查詢部分的章節,如果它真的(!)必要。

+1

你是說'$ this-> paginate'代替'$ this-> validate'嗎? – pleasedontbelong