在 YIi 中建立关系时的条件 [英] condition while making relation in YIi

查看:28
本文介绍了在 YIi 中建立关系时的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代理:

agent_id(主键)

用户:

f_id (foreign key)
type

我以这种方式创建了关系

I have created relation in this way

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

但我想在用户表中添加更多条件,例如仅当 type=3 时才加入.

But I want to add more conditions like join only if type=3 in User table.

谢谢.

推荐答案

为你的关系添加条件

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

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

这篇关于在 YIi 中建立关系时的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆