2013-05-15 80 views
0

這是我的選擇表如何使用find在cakephp中分頁?

id | question_id | content 
1  1    bee 
2  1    fly 
3  1    dog 
4  2    cat 
5  2    bat 
6  2    wasp 

,這是我的問題表

id | content 
1  question1 
2  item2 

這是我did.With這我可以顯示我想要做的choices..what問題現在,是到分頁它,更像是每page.How 1個問題要做到這一點?你的幫助會大大appreciated.Thanks

$options['fields'] = array('questions.id','questions.content'); 
$options['joins'] = array( 
     array(
      'table' => 'generated_exam_items', 
      'alias' => 'GenExamItems', 
      'type' => 'inner', 
      'conditions' => array(
       'GenExamItems.generated_examination_id' => 25 
      ) 
     ), 
     array(
      'table' => 'questions', 
      'alias' => 'Questions', 
      'type' => 'inner', 
      'conditions' => array(
       'Questions.id = GenExamItems.questions_id' 
      ) 
     ), 

    ); 
    $options['conditions']=array('GenExamItems.generated_examination_id' => 25,'Question.id=GenExamItems.questions_id'); 
    $question_detail = $this->Question->find('all',$options); 
    $this->set('questions',$question_detail); 
+0

可能重複[如何獲得每個項目的選擇,在CakePHP的表?](http://stackoverflow.com/questions/16538412/how-to-get-choices-of-each-item -in-a-table-in-cakephp) – Tommy

回答

1

爲了分頁你不應該使用找到呼叫,而是使用分頁呼叫,使用$ this-> paginate設置選項。

$this->paginate = array(
    'joins' => array(
     array(
      'table' => 'generated_exam_items', 
      'alias' => 'GeneratedExamItem', 
      'type' => 'INNER', 
      'conditions' => array(
       'GeneratedExamItem.generated_examination_id' => 25 
      ) 
     ), 
     array(
      'table' => 'questions', 
      'alias' => 'Question', 
      'type' => 'INNER', 
      'conditions' => array(
       'Question.id = GeneratedExamItem.question_id' 
      ) 
     ) 
    ), 
    'conditions' => array(
     'GeneratedExamItem.generated_examination_id' => 25, 
     'Question.id = GeneratedExamItem.question_id' 
    ) 
    'fields' => array(
     'Question.id', 
     'Question.content' 
    ), 
); 

下一頁只是定義像這樣

$this->set('questions', $this->paginate()); 

結果給一個變量我已經改變了一些你的別名,因爲它是很好的做法/慣例使用奇異的模型(即問題),而不是多(問題)作爲別名。此外,使用完整模型名稱(GeneratedExamItem而不是GenExamItem)是一種很好的做法,但如果您的偏好符合您的要求,則可以將其更改回來。

+0

感謝您的幫助 – user2356747

3
$this->paginate = array(
      'Claim' => array(
       'conditions' => array('Claim.user_id' => $user['User']['id']), 
       'fields'=>array('id','description','title','support_count','oppose_count','user_id','created'), 
       'limit' => 5, 
       'paramType' => 'querystring' 
     )); 
     $claim = $this->paginate('Claim'); 
     $this->set('claim',$claim);