Friday, September 19, 2014

HELP cant display records from a join

Hi, I cant get this simple join to produce records. I am trying to link a HABTM model with joins but I dont get any records from the joined tables. 

I want all the subjects for tutor id =2 but as you see I get NULL and I have tried different recursive levels. i should not get NULL in the other tables as you see below. I dont get an error

Here is the controller and model with the relationship

    array(
        (int) 0 => array(
            'Subject' => array(
                'id' => null,
                'name' => null
            ),
            'TutorsSubject' => array(
                'id' => null,
                'tutor_id' => null,
                'subject_id' => null
            ),
            'Tutor' => array(
                'id' => '2',
                'tutor_inactive' => false,
                'first_name' => 'fred2',
                'last_name' => 'blah',..........
   
        class TutorsController extends AppController {
   
         public function tutordetails() {
                    $options2['joins'] = array(
                       array('table' => 'tutors_subjects',
                        'alias' => 'TutorsSubject',
                        'type' => 'LEFT',
                        'conditions' => array(
                        'Tutor.id = TutorsSubject.id',
                         )
                         ),
                       
                        array('table' => 'subjects',
                        'alias' => 'Subject',
                        'type' => 'LEFT',
                        'conditions' => array(
                        'TutorsSubject.subject_id=Subject.id',
                         )
                         )
               
                    ));
                   
                     $options2['fields'] = array('Subject.*','TutorsSubject.*','Tutor.*');
                   
                    $this->Tutor->recursive = -1;
                    $options2['conditions'] = array('Tutor.id'  => 2);
                    $subject=$this->Tutor->find('all',$options2);
                    $this->set('subject', $subject);
                    debug($subject);

   
        class Subject extends AppModel {
        public $hasAndBelongsToMany = array(
           
            'Tutor' => array(
                'className' => 'Tutor',
                'joinTable' => 'tutors_subjects',
                'foreignKey' => 'subject_id',
                'associationForeignKey' => 'tutor_id',
                'unique' => 'keepExisting',
                'conditions' => '',
           
            )
        );
   
    }
   
        class Tutor extends AppModel {
            ..
   
            public $hasAndBelongsToMany = array(
                'Subject' => array(
                    'className' => 'Subject',
                    'joinTable' => 'tutors_subjects',
                    'foreignKey' => 'tutor_id',
                    'associationForeignKey' => 'subject_id',
                    'unique' => 'keepExisting',
                    'conditions' => '',
              
                ),
               
            );
   
   

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: