public function relations(){
return array(
'user' =>array(self::BELONGS_TO, 'User', 'user_id'), // Only visible - use defaultScope
'rawuser' => ... // Visible and invisible users - without defaultScope
);
}
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.
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
);
}
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!