Kohana 3.1 orm。如何使这有一个关系?
我有 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< 的相关记录/code> 表。
我在这里做错了什么吗?如果是,正确的方法是什么?
编辑:
表Country
有PKcode
并且没有FK。
表 Address
具有 PK id
和 FK country_code
I have 2 models: Address
and Country
. Now, every address has exactly one country. So Address
model has:
protected $_has_one = array('country' => array(
'model' => 'Country',
'foreign_key' => 'code',
));
I load Address
object:
$addr = ORM::factory('Address', 1);
$country = $addr->country->find();
But $country
always contains first record instead of a related record from the Country
table.
Am I doing something wrong here? If yes, what's the correct way?
EDIT:
table Country
has PK code
and no FK.
table Address
has PK id
and FK country_code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 has_one 属性应如下所示:
外键是当前模型的表中链接到相关模型的主键的键。
Your has_one property should be as follows:
Foreign key is the key in your table of the current model that links to the primary key of the related model.