named scopes for related models in eager loading problem


I am trying to use named scope in a 'with' call

I apply scopes to the related model.

I think this might be a bug but let's see

the problem :

if no records are returned for the 'filtered' related model, trying to access those (absent records) will trigger a lazy loading on the related model whereas it should not, since we want to be able to account for the fact that there are no records within the named scope.

ex :

2 models



a scope for Event

active (condition : active = 1)

2 events for an Artist (id = 2)

those events are inactive

the eager call :

$a = Artist::model()->with('Event:active')->findByPk(2);

a count on $a->Event should return 0 since there are no active events for Artist 2

but it returns 2

because a lazy load has been performed without scope so all events have been retrieved

setting one of the event as active allows the correct behavior as a count on $a->Event returns 1

I might be missing something

but it seemed to me like this was not an expected behavior


Thanks for reporting this issue. I will look into it.

At the same time, could you please try the following query and see if its result is as expected? Thanks.

$a = Artist::model()->with('Event:active')->together()->findByPk(2);

adding does not return the Artist at all since it has no active event

whereas all that is expected is t have an empty array of events (since none matches the active condition)

Should be fixed now. Please help test. Thanks!

updated to last trunk revision

now 'together' also reports 2 events for Artist 2

just saw your reply

Do I need branch/1.0 ?

yes, branches/1.0.


does not seem to change something to the output

will test again in a few

where should I look for a change in the framework ? so that I know if Iam executing the rigt code (I'll set a breakpoint)

You may verify that line 605 has the following code:

		foreach($this->children as $child)

seems like named scope related models (in with(Model:scope))  is broken in branches/1.0

trunk 993 seems broken too

there are weird results, I have 2 sets of named scopes

Artist::model('Event:active:forthcoming', 'Media:images:active')->findByPk(2);

the first one does not work anymore with rev > 956, the second one does

I reverted to trunk rev 956 and it works ok but for the issue I started the thread with

hope you can make something out of this

Please give more details.

Is your original problem fixed?

For the new problem, what are the definitions for these named scopes? And what is the problem?


So I setup a test project that I have attached.

site/index is where I performed the tests

sql is in protected/data

you can check the application.log where I have stated which logs belongs to which revision

testing with rev 1004 :

  - the Event active named scope ('condition' => ' = 1', 'alias' => 'Event') IS NOT taken into account (no additional condition in sql query)

  - Can not check if a lazy load is performed when cheking for returned events (when no related models should be returned) because since the scope is not taken into account there are always events returned for an artist

testing with rev 956 :

  - the Event active named scope ('condition' => ' = 1', 'alias' => 'Event') IS taken into account

  - when NO events are returned for an artist calling count($artist->events) triggers a lazy load whereas counting active events for an artist which does not have any should return 0, instead it returns the number of all events for this artist regardless of the named scope (because of the lazy load)

maybe I am missing something

thanks a lot for your time


Seems to affect only MANY_MANY relationships where 'condition' is not added to 'on' yet:


`Artist`.`id` AS t0_c0, Event.`id` AS t1_c0, Event.`name` AS t1_c1, Event.`active` AS t1_c2 

FROM `Artist` 

LEFT OUTER JOIN `ArtistEvent` events_Event ON (`Artist`.`id`=events_Event.`Artist_id`) 

LEFT OUTER JOIN `Event` Event ON (Event.`id`=events_Event.`Event_id`) 

WHERE (`Artist`.`id` IN (1, 2))

Which should be:


`Artist`.`id` AS t0_c0, Event.`id` AS t1_c0, Event.`name` AS t1_c1, Event.`active` AS t1_c2 

FROM `Artist` 

LEFT OUTER JOIN `ArtistEvent` events_Event ON (`Artist`.`id`=events_Event.`Artist_id`) 

LEFT OUTER JOIN `Event` Event ON (Event.`id`=events_Event.`Event_id` AND 

WHERE (`Artist`.`id` IN (1, 2))

Thanks! Fix checked in.