开发者

Kohana 3.1 orm. How to make this has_one relashionship?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜