在YIi中建立关系的条件

abn*_*nab 3 yii

代理:

agent_id (primary key)

用户:

f_id (foreign key)
type
Run Code Online (Sandbox Code Playgroud)

我以这种方式创造了关系

public function relations() {
    return array(
        'user' => array(self::HAS_ONE, 'Users', 'f_id'),
    );
}
Run Code Online (Sandbox Code Playgroud)

但是我想type=3在User表中添加更多条件,例如join .

谢谢.

asl*_*gga 8

为你的关系添加条件

public function relations() {
    return array(
        'user' => array(self::HAS_ONE, 'Users', 'f_id', array(
            'condition' => 'user.type = :type',
            'params' => array(':type'=>3)
        )),
    );
}
Run Code Online (Sandbox Code Playgroud)

http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-options

  • 属性"CHasOneRelation.0"未定义.一堆错误的showup.任何线索? (2认同)

bul*_*are 6

没有像"属性"这样的错误CHasOneRelation.0"未定义"如果你使用它:

public function relations()
{
    return array(
        'user' => array(
            self::HAS_ONE, 
            'Users', 
            'f_id',
            'on' => 'user.ref_type = :type', 
            'params' => array(':type' => 3))
    );
}
Run Code Online (Sandbox Code Playgroud)

请看这个链接:http://www.yiiframework.com/forum/index.php/topic/10185-using-relations-and-conditions/