开发者

Kohana 3.1 orm. How to make this has_one relashionship?

开发者 https://www.devze.com 2023-03-18 23:59 出处:网络
I have 2 models: Address and Country. Now, every address has exactly one country. So Address model has:

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 F开发者_如何学CK.

table Address has PK id and FK country_code


Your has_one property should be as follows:

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

Foreign key is the key in your table of the current model that links to the primary key of the related model.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号