Hi.
So, Im stuck with this problem for a while now, even after reading this a few times.
This is how my DB looks like:
tvshows
- id
- name
- description
seasons
- id
- show_id
- name
episodes
- id
- season_id
- name
- description
Basically, A TV show has a few seasons, and every single season has a few episodes.
I have set up my TVShow model relation like this:
public function relations()
{
return array(
'seasons' => array(self::HAS_MANY, 'Season', 'show_id'),
'episodes' => array(self::HAS_MANY, 'Episode', 'show_id', 'through' => 'seasons')
);
}
From a specific TV show I want to select all episodes that belongs to it.
So when I echo out
count(TVShow::model()->with('episodes')->findByPk(1)->episodes)
it returns 1, but it should return at least 100 episodes.
Yii generates this Query
SELECT `episodes`.`id` AS `t1_c0`, `episodes`.`season_id` AS `t1_c1`, `episodes`.`number` AS `t1_c2`, `episodes`.`name` AS `t1_c3`, `episodes`.`description` AS `t1_c4`, `episodes`.`views` AS `t1_c5`, `episodes`.`created` AS `t1_c6`, `episodes`.`updated` AS `t1_c7`
FROM `episodes` `episodes`
LEFT OUTER
JOIN `seasons` `seasons` ON (`seasons`.`show_id`=`episodes`.`id`)
WHERE (`seasons`.`show_id`=:ypl0)
ORDER BY number ASC
I noticed that it generated a wrong JOIN query, It needed to be
LEFT OUTER
JOIN `seasons` `seasons` ON (`seasons`.`id`=`episodes`.`season_id`)
I still dont have any clue after hours, please, any suggestions?
Thanks in advance