Yii Framework Forum: Relation without default scope - Yii Framework Forum

Jump to content

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

Relation without default scope Rate Topic: -----

#1 User is offline   Extak 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 02-February 10
  • Location:Brno, Czech Republic

Posted 30 September 2010 - 07:30 AM

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.
0

#2 User is offline   plasmagunman 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 11-November 10

Posted 31 May 2011 - 11:39 AM

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

#3 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,355
  • Joined: 12-October 09
  • Location:Croatia

Posted 31 May 2011 - 02:50 PM

try to use resetScope() - http://www.yiiframew...setScope-detail
Find more about me.... btw. Do you know your WAN IP?
0

#4 User is offline   plasmagunman 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 11-November 10

Posted 31 May 2011 - 07:40 PM

View Postmdomba, on 31 May 2011 - 02:50 PM, said:

try to use resetScope() - http://www.yiiframew...setScope-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.
3

#5 User is offline   Habichtsberg 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 04-July 11

Posted 01 September 2011 - 01:16 AM

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

#6 User is offline   alex-ks 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 57
  • Joined: 29-August 11
  • Location:Minsk, Belarus

Posted 20 January 2012 - 06:38 PM

View PostHabichtsberg, on 01 September 2011 - 01:16 AM, said:

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

The same problem... Any ideas?
0

#7 User is offline   gesti 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 07-September 11
  • Location:Hungary

Posted 21 January 2012 - 04:47 AM

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')
        ),
    ),
));

0

#8 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 815
  • Joined: 02-July 10
  • Location:Central Poland

Posted 21 January 2012 - 05:54 AM

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
    );
}

red
1

#9 User is offline   Vallu 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 20-July 11

Posted 13 February 2012 - 02:12 PM

View Postredguy, on 21 January 2012 - 05:54 AM, said:

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
    );
}



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!
0

#10 User is offline   Egorio 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 07-July 10

Posted 10 August 2012 - 06:13 AM

Look at github.com/yiisoft/yii/issues/1197.
But I'm not sure that it doesn't hurt anything else.
0

#11 User is offline   rajesh chaurasia 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 12-January 13
  • Location:Mohali

Posted 23 March 2013 - 02:44 AM

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
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