Dynamic Relational Query troubles


From http://www.yiiframework.com/doc/guide/1.1/en/database.arr#dynamic-relational-query-options

In my Post model I have already defined the condition ‘status=1’

public function scopes() {

        return array(






So, How can I use (if it’s possible) my scope ‘active’ instead of array(‘condition’=>‘status=1’)

$posts=$user->posts(/* named scope here? */);



Note that you cannot use parameterized scopes, only those defined in the scopes() method of your related model. See here for details:


You have try this?


or this


Mike this work too, but is not eager loading approach?

I’d like to use the lazy loading approach as in the docs, but instead of


something like


to avoid repeating conditions that I had already defined in scopes()…

I had a look inside CActiveRecord and seems that it doesn’t support this kind of call (with non-parameterized named scopes)…would be soooo good to have!

Ah, right, missed that you where talking about lazy loading. From looking at the source of CActiveRecord::__call() i’d also say it’s not possible but could maybe be implemented as a feature. getRelated() could therefore accept $params to also be a string and interprete this as name of a named scope.

With some luck such a feature request might get accepted.

all right! hope to get lucky…



yeah! fixed!