I'm trying to work with relational queries and having a bit of trouble.
My tables are set up as follows:
Games:
id (pk)
Grades:
id (pk)
SiteGames:
site_id (pk),
game_id (pk),
game_id references Games.id
GradeGames:
grade_id (pk),
site_id (pk),
game_id (pk),
grade_id references Grades.id,
(site_id, game_id) references (SiteGames.site_id, SiteGames.game_id),
game_id references Games.id,
The basic idea is that there are a group of games and some subset of that group is available to a certain site. The games available to the site can be customized even further for each grade level. Grades cannot have a game if the game is not available to their site.
In my Grades model, I have the following relation:
'availableGames' => array(self::MANY_MANY, 'Games', 'GradeGames(grade_id,game_id)'),
When running the following line of code, I get an error:
$gradeGames = Grades::model()->with('availableGames')->together()->findByPk($student->grade_id);
The relation "availableGames" in active record class "Grades" is specified with an invalid foreign key "game_id". The foreign key does not point to either joining table.
I ended up removing the foreign key '(site_id, game_id) references (SiteGames.site_id, SiteGames.game_id)' from GradeGames and this fixed the problem.
Anybody have any idea what the problem is? I have several situations like this and don't want to have to re-order/remove my foreign keys. Plus I'd like to know what the issue is anyway since I'm trying to understand Yii better. Thanks.