Relation without default scope

Hi,

i trying to do some relation:




public function relations(){

    return array(

		'user' =>array(self::BELONGS_TO, 'User', 'user_id'), // Only visible - use defaultScope

                'rawuser' => ... // Visible and invisible users - without defaultScope

    );

}



User model has defaultScope:




array(

    'alias'=>$this->tableName(),

    'condition'=>$this->tableName().'.visible=1',

);



Is there any way to get related model without useing defaultScope like $model->rawuser? System most of time needs only visible users, but in some situations it is unrelevant if user is visible or not.

has anyone solved this? i have the same problem right now…

try to use resetScope() - http://www.yiiframework.com/doc/api/1.1/CActiveRecord#resetScope-detail

you can use resetScope() on CActiveRecord instances, but i want to use it on relations, e.g.:

normal access is:




$model->relation // with default scope, which is defined in relation's model class



only in few cases it should be:




$model->relation // without relation's model default scope



i can reset the scope of $model, but




$model->resetScope()->relation



doesn’t reset the scope defined in relation’s class, and




$model->relation->resetScope()



doesn’t work, since the request on relation’s model is made before resetScope() is executed.

I have the same prob’ right now - a solution for this would be great

The same problem… Any ideas?

My idea, is to make a scope that resets all the previous scopes, and than you can call on it like this:


$posts=Post::model()->with('comments:recently:approved')->findAll();

// or since 1.1.7

$posts=Post::model()->with(array(

    'comments'=>array(

        'scopes'=>array('recently','approved')

    ),

))->findAll();

// or since 1.1.7

$posts=Post::model()->findAll(array(

    'with'=>array(

        'comments'=>array(

            'scopes'=>array('recently','approved')

        ),

    ),

));

resetScope function is designed for pipeline usage, so you should be able to use like scope:




public function relations(){

    return array(

                'user' =>array(self::BELONGS_TO, 'User', 'user_id'), // Only visible - use defaultScope

                'rawuser' => array(self::BELONGS_TO, 'User', 'user_id', 'scopes'=>array( 'resetScope' ) ) // Visible and invisible users - without defaultScope

    );

}



however if you need to access all records in one scenario and only limited rows in other - why don’t you reverse the problem and create standard scope instead of defaultScope?




public function relations(){

    return array(

                'user' =>array(self::BELONGS_TO, 'User', 'user_id', 'scopes'=>array( 'visible' )), // Only visible - use scope

                'rawuser' => array(self::BELONGS_TO, 'User', 'user_id' ) // Visible and invisible users - without any scopes

    );

}



Has someone done this successfully?

I don’t seem to get any effect on generated SQL by using ‘scopes’ => ‘resetScope’.


Event model relations():

	'job' => array(self::BELONGS_TO, 'Job', 'job_id', 'scopes' => array('resetScope')),




Job model:

	public function defaultScope() {

		return array(

			'condition' => '"'.$this->getTableAlias(false, false) . '"."deleted" IS NULL',

		);

	}




Querying SQL: SELECT "job"."id" AS "t1_c0", "job"."title" AS "t1_c1", ...shortened... FROM "job" "job"  WHERE ("job"."deleted" IS NULL) AND ("job"."id"=:ypl0)



On the other hand if I change ‘scopes’ => ‘resetScope’ to a named scope, e.g. ‘scopes’ => ‘active’, then it is added to the SQL just fine. Why not resetScope? Does it work for you guys? I’m using Yii v1.1.10. Thanks!

Look at github.com/yiisoft/yii/issues/1197.

But I’m not sure that it doesn’t hurt anything else.

Hi friends i am using

public function defaultScope()

{


		if(Yii::app()->user->isAdmin())


		return array( 'order'=> 'id DESC',); 


	if(Yii::app()->user->isGuest)


		return array( 'order'=> 'id DESC', 'condition'=> 'status_id =1');


	return array( 'order'=> 'id DESC', 'condition'=> 'status_id = 1 AND email= \''.Yii::app()->user->getModel()->email.'\''


			.' OR id = \''	. Yii::app()->user->id.'\'' );


}

try it it works fine. :D :D

Hi all.

I had the same problem and came with a simple solution. See my manual:

http://www.yiiframework.com/wiki/462/yii-for-beginners-2/#hh16

Enjoy :slight_smile: