Advertisement
nrogers64

Stack Overflow Question: CakePHP Containable with HABTM

Dec 11th, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.96 KB | None | 0 0
  1. // See http://stackoverflow.com/questions/13826474/cakephp-containable-with-habtm
  2.  
  3. Controller::loadModel('Student');
  4. Controller::loadModel('Course');
  5. Controller::loadModel('CoursesStudent');
  6.  
  7. $this->Student->bindModel(
  8.     array('hasAndBelongsToMany' => array(
  9.         'Course'
  10.     ))
  11. );
  12.  
  13. $this->Course->bindModel(
  14.     array('belongsTo' => array(
  15.         'Teacher'
  16.     )),
  17.     array('hasAndBelongsToMany' => array(
  18.         'Student'
  19.     ))
  20. );
  21.  
  22. $this->CoursesStudent->bindModel(
  23.     array('belongsTo' => array(
  24.         'Course',
  25.         'Student'
  26.     ))
  27. );
  28.  
  29. $this->request->data = $this->Student->CoursesStudent->find('all', array(
  30.     'recursive' => 2,
  31.     'conditions' => array(
  32.         'CoursesStudent.is_approved' => null,
  33.         'Student.grade' => 9
  34.     ),
  35.     'contain' => array(
  36.         'CoursesStudent',
  37.         'Course' => array(
  38.             'Teacher' => array(
  39.                 'fields' => array(
  40.                     'Teacher.id',
  41.                     'Teacher.first_name',
  42.                     'Teacher.last_name'
  43.                 )
  44.             )
  45.         ),
  46.         'Student'
  47.     )
  48. ));
  49.  
  50. pr($this->request->data);
  51. exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement