【发布时间】:2015-02-12 14:00:13
【问题描述】:
在以下情况下尝试设置关系时我遇到了麻烦。
我有一张桌子Person (id_person, name, id_mother, id_father)
我知道在模型中与不同表的外键建立关系很热。但在这种情况下,id_mother 和 id_father 映射到其他 id_person(希望我说清楚了)。
这是我尝试过的
public function relations() {
return array(
'father'=>array(self::BELONGS_TO, 'PERSON', 'id_person'),
'mother'=>array(self::BELONGS_TO, 'PERSON', 'id_person'),
);
}
我想要他们在 CGridVew 中的名字
$data->father->NAME
$data->mother->NAME
在这种情况下我该怎么办?
【问题讨论】:
-
在这种情况下你有延迟加载。尝试将属性纳入标准或将属性设置为 true ('father'=>array(self::BELONGS_TO, 'PERSON', 'id_person', 'together'))。您也可以使用: $data->father ? $data->father->NAME : '';
-
一起工作,但是当 id_mother 和 id_father 为空时我该怎么办?它说关系 0 没有定义
-
然后测试 if (isnull($data->father->NAME)) ....