0

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
ajt
  • 642
  • 2
  • 6
  • 21

1 Answers1

0

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;

Yaroslav
  • 2,338
  • 26
  • 37
  • Thanks for the reply but again I am confuse about this. I am talking about a HABTM relationship. The docs clearly say you must use a join for such a relationship and you are saying you dont? What is the conventional method to display data from BOTH tables in a HABTM relationship. book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm – ajt Aug 31 '14 at 02:07
  • I tried the above answer and it isnt an answer could we take that down. I already have the containable. HABTM is a different method. I even tried tthis without success as expected since HABTM is different but where is the docs on a complete method on this? $student= $this->Lesson->find('all', array('contain' => array( 'Student' , 'Tutor'))); – ajt Aug 31 '14 at 08:09
  • Solved as I need an extra fields array which the docs dont specify. $options['fields'] = array('Student.*','Lesson.*','LessonsStudent.*'); $options['conditions'] = array('Student.id' => 2); – ajt Aug 31 '14 at 08:53
  • `fields` is optional, if you don't specify `fields` cake will fetch all fields of all tables. For HBTM it's need to join, but when you're writing model association cake will do joins for you internally. Just set `debug = 2` in `core.php` config and see what queries was executed. – Yaroslav Sep 01 '14 at 09:56