I read the section Containable and I didn't find a clear example to replace an inner join query on a habtm relationship by a containable query. Example :
Model
Student hasAndBelongsToMany Teacher
Teacher hasAndBelongsToMany Student
Query
$joins = array(
array(
'table' => 'teachers_students',
'type' => 'INNER',
'conditions' => array(
'teachers_students.teacher_id' => $teacherId,
'teachers_students.student_id = Student.id'
)
)
);
$data = $this->find('all', array('joins' => $joins));
Comments
- The
hasAndBelongsToMany
attributes are set in each model. - pseudo-variable
$this
reference to model :class Student
$teacherId
is a parameter (there is a filter to show students that belong to one specific teacher).
What I'm looking for
To be able to write the same query without joins
, using contain
. Something like :
$contain = array(
'Teacher' => array(
'conditions' => array('???' => '???')
)
);
$data = $this->find('all', array('contain' => $contain));