我这里有一个情况 有两种型号的用户,帖子 现在我想通过用户&帖子中调整的关系,或通过添加到列表发布的用户,其用户类型那些帖子= 1 这是可能的任何类型的范围?警予关系活动记录
我现在
User
'Posts' => array(self::HAS_MANY, 'Post', 'userId'),
Posts
'User' => array(self::BELONGS_TO, 'User', 'userId'),
我这里有一个情况 有两种型号的用户,帖子 现在我想通过用户&帖子中调整的关系,或通过添加到列表发布的用户,其用户类型那些帖子= 1 这是可能的任何类型的范围?警予关系活动记录
我现在
User
'Posts' => array(self::HAS_MANY, 'Post', 'userId'),
Posts
'User' => array(self::BELONGS_TO, 'User', 'userId'),
您可以命名范围或关系做到这一点。例如,你可以定义你的关系是这样的:
'Posts' => array(self::HAS_MANY, 'Post', 'userId', 'condition' => 't.userType=1')
关于“命名范围”读到这里: http://www.yiiframework.com/doc/guide/1.1/en/database.ar#named-scopes
您可以在飞行中添加的过滤器为命名范围调整的关系定义,像这样的:
class User extends CActiveRecord
{
public function relations()
{
return array(
'Posts'=>array(self::HAS_MANY, 'Post', 'userId',
'with'=>array(
'User'=>array(
'scopes'=> array(
'userType' => 1,
),
),
),
),
);
}
}
更多信息可以在Yii的指南中找到为relational-query-with-named-scopes
不,这不是一个可行的解决方案。我认为这个关系需要调整 'User'=> array(self :: BELONGS_TO,'User','userId'), – iThink 2011-12-20 07:52:20