如何使用find在cakephp中进行分页?

时间:2013-05-15 03:02:19

标签: cakephp pagination cakephp-2.0 cakephp-2.3

这是我的选择表

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

这就是我所做的。有了这个,我可以用它的选择显示问题。我现在要做的就是分页,更像是每页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);

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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());

我已经更改了一些别名,因为使用Singular模型(即Question)而不是复数(Questions)作为别名是一种很好的做法/惯例。此外,最好使用完整的型号名称(GeneratedExamItem而不是GenExamItem),但如果符合您的喜好,您可以将其更改回来。