我有 2 个模型:Address
和 Country
。现在,每个地址都只有一个国家/地区。所以 Address
模型有:
protected $_has_one = array('country' => array(
'model' => 'Country',
'foreign_key' => 'code',
));
我加载Address
对象:
$addr = ORM::factory('Address', 1);
$country = $addr->country->find();
但是 $country
始终包含第一条记录,而不是 Country
表中的相关记录。
我在这里做错了什么吗?如果是,正确的方法是什么?
编辑:
表Country
有PKcode
并且没有FK。
表 Address
具有 PK id
和 FK country_code
最佳答案
您的 has_one 属性应如下所示:
protected $_has_one = array('country' => array(
'model' => 'Country',
'foreign_key' => 'country_code',
));
外键是当前模型表中链接到相关模型主键的键。
关于Kohana 3.1 orm。如何建立这个 has_one 关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6640829/