Resetscope On Relation

Hi there,

For security reason I am using a default scope with the owner

This is in the model ClientDocument default scope:





    public function defaultScope() {

        return array(

            'condition' => 'id_user = ' . Yii::app()->user->id,

        );

    }



This model is used in a relation in model Client:




public function relations() {

        return array(          

            'clientDocuments' => array(self::HAS_MANY, 'ClientDocument', 'id_client'),

        );

    }



So I can use $client->clientDocuments to access the documents.

Now the problem I have is that in my administrator view I want to see and approve the documents.

But I can’t find a way to reset the scope. I get an error when I call $client->clientDocuments (SQL error, it’s using WHERE id_client = ‘admin’).

Any idea?

Thanks

Renaud

Hi

Following a discussion on GitHub, I post my workaround here too.

My workaround is to define a new class extending the model class in order to remove the scope exceptionnaly and to reference that class in the relation:


class EntityNoDefaultScope extends Entity {

	public function defaultScope() {

		return array();

	}

}



So with the above replacement, I use ‘EntityNoDefaultScope’ in stead of ‘Entity’ in the relation where I do not want the default scope. Implementation is simple, not intrusive, clear, uses limited resources and does not interfere with the framework.

[color="#333333"][font="Helvetica, arial, freesans, clean, sans-serif"] [/font][/color]