Using Relations With Scope And Select Attributes - Not Working


Little bit passed beginner stage with Yii I got stuck at this not working. My version is 1.1.15 btw.

Each User model has_one Profile model. And I’m trying to process a limited API call so I want to retrieve a certain set of data from the profile only.

I have the relation defined in User model:

public function relations()


        $relations = Yii::app()->getModule('user')->relations;

        if (!isset($relations['profile']))

            $relations['profile'] = array(self::HAS_ONE, 'Profile', 'user_id');

        return $relations;


Then I have the following scope set in Profile model:

public function scopes()


        return array(

           'api' => array(

                'select' => 'photoURL, displayName',




And finally in my ApiController I call upon this the following way:

$data = User::model()->with('profile:api')->findByPk(Yii::app()->user->id);

This actually works. But the problem is the scope is not applied to the Profile pulled in. I still have all the other columns as well. Some documentation/forums said the other columns will be returned with "null’ values but it’s not even that, they’re all filled out properly and I don’t want to return all information to the API (due to security reasons as well).

Can you please advise why this is not working for me? I found two more posts with no answers.

try this


    'condition'=>"id = :ID",

    'params'=>[':ID'=> Yii::app()->user->id],





Thank you for this, but still no luck. I had to alter the syntax to get it working, but it the scope still does not apply.

Also I’d prefer the use the scopes in the original way I presented it, much much cleaner. I am aware I can create a function in the model to achieve the same result and can use the same syntax (->function()->findByPk()), but I want to see if I can get it working with scopes only.

Any way to debug to check if the scope definition is correct, and if yes it is being applied?