Kohana 3.1 orm。如何建立这个 has_one 关系?

标签 kohana kohana-orm

我有 2 个模型:AddressCountry。现在,每个地址都只有一个国家/地区。所以 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/

相关文章:

kohana - 如何在 Kohana ORM 中使用 BETWEEN 子句

kohana - PHPUnit + Kohana : Undefined index: HTTP_HOST

PHP向数据库插入数据

git submodule init 忽略失败

php - Kohana 3.2 中的 $this->request->param() 和 $this->request->post()

mysql - kohana框架中如何进行原始查询

php - 钩子(Hook)……它们到底是什么

kohana - 基本 Kohana ORM 查找记录

php - Kohana 3.1 有哪些 ORM 解决方案?

php - 是否可以为行数重新使用 Kohana ORM 查询?