Parties are loaded with correct order, however additional query to eagerly load the members is not created and only one or zero(if there was no members for a specific party) is preloaded. This means that not only dont I get all the members, but lazy loading is not invoked either.
SELECT `t`.`id` AS `t0_c0`, `t`.`founder_id` AS `t0_c1`, `t`.`president_id` AS `t0_c2`, `t`.`name` AS `t0_c3`, `t`.`country_id` AS `t0_c4`, `members`.`id` AS `t1_c0`, `members`.`username` AS `t1_c1`, `members`.`password` AS `t1_c2`, `members`.`firstname` AS `t1_c3`, `members`.`surname` AS `t1_c4`, `members`.`country_id` AS `t1_c5`, `members`.`region_id` AS `t1_c6`, `members`.`citizenship_id` AS `t1_c7`, `members`.`email` AS `t1_c8`, `members`.`party_id` AS `t1_c9` FROM `parties` `t` LEFT OUTER JOIN `citizens` `members` ON (`members`.`party_id`=`t`.`id`) GROUP BY t.id ORDER BY COUNT(members.id) DESC
Sorting in php is not a very good idea, because it means bye-bye pagination - a huge performance hit. I think it is a bug, because otherwise it shouldnt bind any members to the party, but it does do that for the one thats returned with the party row.
If this is the normal behaviour, not a bug, is there any way to bind members to the party set manually?
Did some research as that it seems to be a quite discussed thing and my solution is not good (creating STAT queries). Please refer to this article where zac and Mike speak about a way of doing it by using $criterias.
$parties = Party::model()->findAll(
'select'=>array('*', 'COUNT(members.id) as mcount'),
// then load members when needed
foreach($parties as $party)
foreach( $party->members as $member)