I have two models: Users
and Lessons
User
has oneTeacher
User
has and belongs to manyStudents
Both Teachers
and Students
are Users
In my Users
model I have this association:
public $hasMany = array(
'Lesson' => array(
'className' => 'Lesson',
'foreignKey' => 'user_id',
'dependent' => false,
)
);
In my Lessons
model I currently have this association:
public $belongsTo = array(
'Teacher' => array(
'className' => 'User',
'foreignKey' => 'teacher_id',
'conditions' => array('Teacher.group_id' => '2'),
'fields' => '',
'order' => ''
)
);
My questions are:
Why does
$teachers = $this->Lesson->Teacher->find('list');
return all theusers
and not just theteachers
? Why don't the conditions set in thebelongsTo
part of the model filter the results to show only theUsers
with thegroup_id
of 2 i.e. theTeachers
? And if that's not the way it's supposed to work then what is the point of setting conditions here?As of now I have to include it in the find each time which seems kind of redundant:
$teachers = $this->Lesson->Teacher->find('list', array('conditions'=>array('Teacher.group_id'=>'2')))
How do I write the associations for
Students
so thatLessons
has many students? And how do I set up the database for it?