AR Named Scopes and Select Clause

Hi, I have some problem in understandog how named scopes works.

In particular i have a user model whit lots of field and a view where I need only some of them ordered by last name.

In my model class file I put




    public function scopes()

    {

        return array(

            'summary'=>array(

                'select'=>array('id', 'first_name', 'last_name', 'email'),

                'order'=>'last_name ASC',

            ),

        );

    }

and in the controller file




$users = Users::model()->summary()->findAll();



Now the problem is that records are rightly ordered but still all record fields are returned instead of only the 4 in the select parameter of the "summary" scope defined.

Do I miss something?

Thanks and sorry for the newbie question

alex

first of all change Users model to User, because it represent a single User object

and you should look at trace and profile to see what query executed…

Actually I’m not sure why it not working… but I would try in your place to specify condition:

public function findAll($condition=’’,$params=array())

Thanks a lot for your answer.

I solved with some tracing and further reading of the api docs.

The problem was that after fetching model I use getAttributes() to get column data which require null as parameter to get only selected fields as mentioned in the corresponding api doc.

Sorry and thanks again

I’m having a similar issue. I was having the same issue with getAttributes() but I’m new and didn’t even realize that was an alternative to just using the ->attributes member of the AR class. Now, this fixes the issue with direct model lookups but I’m still having the same issue with related looks. I created two classes WIDGETS and SUBWIDGETS. WIDGETS have many SUBWIDGETS. SUBWIDGETS have a scope named “summary”. If i get

SUBWIDGETS::model()->summary()->findAll

and then use

getAttributes(null)

to retrieve the data it all works as planned but when I go to retrieve

WIDGETS::model()->with(‘SUBWIDGETS:summary’)

it still selects all of the columns for SUBWIDGETS. Is there any way to just retrieve the summary data? Any help greatly appreciated.

After a bit more research I realize that the scope is actually working but when accessing the related data it still has all of the fields as attributes but the ones not selected in the scope are just left null. Is there any way to avoid having these attributes at all? Thanks!