I have two models as follows:
MyuserClassroom - db table is called 'myuser_classrooms'
MyuserClassroomPivot - db table is called 'myuser_classrooms_pivot'
The structure is quite simple
myuser_classrooms(classroom_id, title, organisation_id, active)
myuser_classrooms_pivot(classroom_id, user_id)
Classroom ID in the pivot table is the foreign key (as is the user_id for the users table)
In the MyuserClassroom model I have the following relation
function relations() {
return array(
'classroomStudents' => array(self::MANY_MANY, 'MyuserClassroomPivot', 'myuser_classrooms_pivot(classroom_id, user_id)')
)
}
I simply want a collection of all of the rows in the pivot table by classroom_id but it doesn’t work for me as I get an empty array.
Can anyone suggest what I am doing incorrectly?
This is an sql fiddle of the database
http://sqlfiddle.com/#!2/37121f/1
// below is the query that Yii generates in the log of queries…
SELECT
`classroomStudents`.`classroom_id` AS `t1_c0`,
`classroomStudents`.`user_id` AS `t1_c1`
FROM
`myuser_classrooms_pivot` `classroomStudents`
INNER JOIN `myuser_classrooms_pivot` `classroomStudents_classroomStudents` ON (
`classroomStudents_classroomStudents`.`classroom_id` = 4
)
AND (
`classroomStudents`.`classroom_id` = `classroomStudents_classroomStudents`.`user_id` )