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.
EDIT:
tableCountry
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.
精彩评论