0
votes

I just cant get data from 2 tables in a HABTM relationship and I cant get the answer for these cakephp docs (again). Sorry for asking the question on something that should be explained in the docs. This should be a simple but I keep getting undefined index and the relationship I set up seems in accordance with the docs. I have another post on a more complicated matter but this issue needs to be isolated .

I dont know how to get related data from the lessons table and the students table via this lessons-students HABTM table.

I want all the lessons a student does. I want the name of the student from the student table and lesson details from the lesson table which has lesson_id as the common link in this lessons-students table. Sounds simple but I cant do it.

public $hasAndBelongsToMany = array(

        'Student' => array(
            'className' => 'Student',
            'joinTable' => 'lessons_students',
            'foreignKey' => 'lesson_id',
            'associationForeignKey' => 'student_id',
            'unique' => 'keepExisting',

        )
    );




class LessonsController extends AppController {

....
$this->Lesson->recursive = -1;

  $options['joins'] = array(
               array('table' => 'lessons_students',
                'alias' => 'LessonsStudent',
                'type' => 'LEFT',
                'conditions' => array(
                'Lesson.id = LessonsStudent.lesson_id',
                 )
                 ),

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

            );

            $options['conditions'] = array('Student.id' => 2);
      //  $Item->find('all', $options);

     $student=$this->set( 'student',$this->Lesson->find('all', $options));


In the view I get the error undefined index Student
   <?php 
    //  debug($student);
      foreach ($student as $item2):
           echo '<td>'. $item2['Lesson']['id'].'</td>';    
        echo '<td>'. $item2['Student']['id'].'</td>';



http://stackoverflow.com/questions/17250215/cant-get-all-user-data-from-habtm-in-cakephp
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm
1

1 Answers

0
votes

If you want to use model association you need to remove $this->Lesson->recursive = -1; because it disable any associations defined in model. And than you can remove $options['joins']. If you have defined many association in your Lesson model and don't want to fetch all of it use unbindModel() or use Containable behavior

$this->Lesson->Behaviors->load('Containable');
$this->Lesson->contain('Student');

If you need to do find many times in many actions it's better to enable Containable in model itself

class Lesson extends AppModel {
    public $actsAs = array('Containable');
}

so you can avoid $this->Lesson->Behaviors->load('Containable'); line;