Friday, August 29, 2014

find all on complex model

I just need to get the details from a find all across 4 associated tables. I looked at the docs and I can get the data from 2 tables but not the other tables.
I have a lessons table where I get the relevant tutor id rows to display only. This works fine. I have a lessons-students table related to the lessons table over the FK lessons id. From this lessons-students table I then a field linked to another table.

lessons table (fk tutor_id but no student_id)
tutors table(linked to lessons table with tutor_id)
lessons_students table (linked with lessonID and has a studentID)
students table(linked with studentID from lesson-students table)

so if I get a row from lessons table I want the tutor name (from tutor table and I can do this now), the student name via the studentID . This is 4 tables and this is proving a headache as the docs just do simple examples.

http://book.cakephp.org/2.0/en/models/retrieving-your-data.html

    class LessonsController extends AppController {
       $this->set( 'tutor',$this->Lesson->find('all', array(
                 'conditions'=>array('Lesson.Tutor_id'=> 1,'Lesson.id'=>'Lesson_Students.lesson_id') ) ));
            
        
   
    view
    ////////
   
      <?php
          foreach ($tutor as $item):
               
            echo '<tr><td>'. $item['Lesson']['id'].'</td>';
          //  echo '<td>'. $item['Tutor']['id']['username'].'</td>';
            echo '<td>'. $item['Lesson']['tutor_id'].'</td>';
             echo '<td>'. $item['Tutor']['last_name'].'</td>'; 
               echo '<td>'. $item['Lesson_Student']['student_id'].'</td>';
            echo '</tr>';
         endforeach;
         unset($item);


--
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: