【问题标题】:Kohana 3.1 orm. How to make this has_one relashionship?Kohana 3.1 orm。如何建立这种 has_one 关系?
【发布时间】:2011-07-10 11:34:27
【问题描述】:

我有 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 有 PK code 并且没有 FK。
Address有PKid和FKcountry_code

【问题讨论】:

  • 1 个地址正好有一个国家和 1 个国家属于多个地址是 one-to-many 关系,而不是 one-to-one
  • 但是Country 不能属于任何东西,因为它是所有已知国家的唯一实用表,并且被许多其他表引用。
  • 只要一个国家属于任意数量的地址 - 它就是one-to-many 关系。 rtfm
  • 你是对的。我已经编辑了主题。
  • 使用$addr->country(不带find())。

标签: kohana kohana-orm


【解决方案1】:

您的 has_one 属性应如下所示:

protected $_has_one = array('country' => array(
  'model' => 'Country',
  'foreign_key' => 'country_code',
));

外键是当前模型表中链接到相关模型主键的键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 2013-01-27
    相关资源
    最近更新 更多