Yii: 如果我更改它的名字,BELONGS_TO关系会返回 null


Yii: BELONGS_TO relation returns null if I change its name

我想知道为什么框架会有这种奇怪的行为。

如果我在我的事件模型中将关系定义为 weird 或除 interest 以外的任何其他名称,它可以正常工作地获取类 Interest 的对象。

public function relations() 
{
    return array_merge(
        parent::relations(),
        array(
            'weird' => array(self::BELONGS_TO, 'Interest', 'interest_id'),
        ));
}

但是如果我将名称更改为 interest它会返回null

public function relations()
{
    return array(
        'interest' => array(self::BELONGS_TO, 'Interest', 'interest_id'),
    );
}

因此,简单地将名称更改为利息意味着关系将返回null

事件中是否有任何名为兴趣的变量,您不能用 Yii 特殊(如关系和魔法方法)覆盖原生类变量。如果没有,那么这里正在发生一些更奇怪的事情。