0

这是我的选择表

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 个问题。如何做到这一点?您的帮助将不胜感激。谢谢

$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);
4

2 回答 2

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);
于 2013-10-19T14:37:38.503 回答
1

为了分页,你不应该使用 find 调用,而是使用 paginate 调用,使用 $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)是一种很好的做法,但如果您愿意,可以将其更改回来。

于 2013-05-15T19:37:59.887 回答