Yii Framework Forum: Resetscope On Relation - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Resetscope On Relation Rate Topic: -----

#1 User is offline   Renaud 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 04-October 11

Posted 08 April 2013 - 05:27 PM

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
0

#2 User is offline   le_top 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 352
  • Joined: 08-June 10
  • Location:France

Posted 22 May 2013 - 01:27 PM

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.



0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users