不能从cakephp分页加入获得不同的行

时间:2015-04-19 07:46:47

标签: php cakephp

我无法从此搜索中获得不同的学生ID行。 我没有收到错误,返回的数据是正确的,因为有些学生有超过1个科目。 主题的原因是稍后包含搜索参数,因此我仍然需要联接中的主题。 这似乎什么都没做?'DISTINCT Student.id'

 $this->Student->recursive = -1;

        $joinoptions = array(

             array('table' => 'students_subjects',
                        'alias' => 'StudentsSubject',
                        'type' => 'LEFT',
                        'conditions' => array(
                        'Student.id=StudentsSubject.student_id',
                 )
                 ),     

           array('table' => 'guardians',
                'alias' => 'Guardian',
                'type' => 'LEFT',
                'conditions' => array(
                'Student.guardian_id=Guardian.id',
                 )
                 ),
           array('table' => 'subjects',
                        'alias' => 'Subject',
                        'type' => 'LEFT',
                        'conditions' => array(
                        'StudentsSubject.subject_id=Subject.id',
                 )
                 ),           

            );

         $fieldoptions = array('DISTINCT Student.id',' Student.last_name','Student.first_name',
             'Guardian.guardian_last_name','Guardian.guardian_first_name','Guardian.id','Guardian.guardian_mobile','Guardian.guardian_email',
                     'Subject.name','Subject.id'
             );

               $condionoptions= array(' Student.student_enq !=' => true,'Student.student_inactive !=' => true,'Student.tutor_gender_preference !=' => '', 

                      'AND' =>array(
                      array('OR' => array(
                                   array('Student.first_name LIKE'  => '%' . $searchFirstName  . '%'),
                                   array('Guardian.guardian_first_name LIKE'  => '%' . $searchFirstName  . '%'),

                        )),
                          /*
                      array('OR' => array(
                                   array('Student.last_name LIKE'  => '%' . $searchLastName  . '%'),
                                   array('Guardian.guardian_last_name LIKE'  => '%' . $searchLastName  . '%'),

                        )) ,
                        array('OR' => array(
                                   array('Student.student_mobile LIKE'  => '%' . $searchmobile  . '%'),
                                   array('Guardian.guardian_mobile LIKE'  => '%' . $searchmobile   . '%'),

                        ))
                         */ 
                        ) 
                       );


          $orderoptions =array('Student.first_name'=> 'ASC');             


        $this->Paginator->settings = array(
         'conditions'=> $condionoptions,
               'fields'=>$fieldoptions,
                'joins'=> $joinoptions,
                'limit' => 25,
        'recursive' =>-1,
                'order'=> $orderoptions,
               'page'=>1
               ); 

          $students= $this->Paginator->paginate('Student');  

示例输出

(int) 1 => array(
        'Student' => array(
            'id' => '216',
            'last_name' => 'Nipps',
            'first_name' => 'Aaron'
        ),
        'Guardian' => array(
            'guardian_last_name' => 'Nipps',
            'guardian_first_name' => 'Audet',
            'id' => '216',
            ..
        ),
        'Subject' => array(
            'name' => 'English: Year 7 - 10',
            'id' => '9'
        )
    ),
    (int) 2 => array(
        'Student' => array(
            'id' => '216',
            'last_name' => 'Nipps',
            'first_name' => 'Aaron'
        ),
        'Guardian' => array(
            'guardian_last_name' => 'Nipps',
            'guardian_first_name' => 'Audet',
            'id' => '216',
            ..'
        ),
        'Subject' => array(
            'name' => 'Maths: Year 7 - 10',
            'id' => '16'
        )
    ),

CakePHP: How to make the paginator component use distinct counting?

EDIT
  (int) 1 => array(
            'Student' => array(
                'id' => '216', //Only want ids to appear once and not twice as above, The above example has the id 216 appearing 2 times
                'last_name' => 'Nipps',
                'first_name' => 'Aaron'
            ),
            'Guardian' => array(
                'guardian_last_name' => 'Nipps',
                'guardian_first_name' => 'Audet',
                'id' => '216',
                ..
            ),
            'Subject' => array(
                'name' => 'English: Year 7 - 10',
                'id' => '9'
            )
        ),

1 个答案:

答案 0 :(得分:0)

我需要添加组,这解决了问题

 $this->Paginator->settings = array(
         'conditions'=> $condionoptions,
               'fields'=>$fieldoptions,
                'joins'=> $joinoptions,
                'group' => 'Student.id',
                'limit' => 25,
        'recursive' =>-1,
                'order'=> $orderoptions,
               'page'=>1
               );