不能连接表HABTM和多对一,cakephp

时间:2016-03-18 10:46:32

标签: php cakephp

我无法使用连接从

的表关系中获取所需的数据

学生HABTM科目,和 守护者1对多学生

如果没有提供所有代码,我的find会获得所需的数据,但它会添加另一个表(AvailabilityForStudent),该表与Student以及其他字段具有HABTM关系。我只是得到太多的数据。 我必须将Guardian2添加到Guardian表中以避免我不理解的冲突。

仅显示3个表中数据的正确连接是什么?

   $students = $this->find('all', array(
            'joins'=>$joins,
                'conditions'    => $conditions,
              'fields'=> $fields,

              'order'=>$order,
            ));


      $joins = array(

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

                );



    (int) 0 => array(
        'Student' => array(
            'id' => '267',
            'last_name' => 'xxx',
            'first_name' => 'xxx',
            'address_suburb' => 'xxx',
            'student_inactive' => false,
            'student_mobile' => '0'
        ),
        'Guardian' => array(
            'guardian_last_name' => 'xx',
            'guardian_first_name' => 'xxxx',
            'id' => '267',
            'guardian_mobile' => 'xxxx',
            'guardian_email' => 'xx@yahoo.com.au'
        ),
        'Subject' => array(
            'name' => 'English: Year 7 - 10',
            (int) 0 => array(
                'id' => '9',
                'name' => 'English: Year 7 - 10',
                'StudentsSubject' => array(
                    'id' => '1079',
                    'student_id' => '267',
                    'subject_id' => '9',
                    'created' => null,
                    'modified' => null
                )
            )
        ),
        'StudentsSubject' => array(
            'id' => '1079'
        ),
        'AvailabilityForStudent' => array(
            (int) 0 => array(

更新 - 添加此行$ this-> recursive = -1而不是$ this-> Student-> recursive = -1;它的工作原理

1 个答案:

答案 0 :(得分:0)

更新 - 添加此行$ this-> recursive = -1而不是$ this-> Student-> recursive = -1;它的工作原理