Relational AR, HAS_ONE on the non PK column

Hi guys!

I have table with user profile and table with countries, profile table has field country_id that is not PK. But I want to retrieve user country with relational AR so I tried to map it using HAS_ONE

'location' => array(self::HAS_ONE, 'Location', 'id'),

But it doesn't work. I think because it tries to map  profile.id to location.id

How can I map it correctly? profile.country_id to location.id?

I've spent all morning trying but failed…

Wait a moment.

is country_id a FK in Profile?

‘location’ => array(self::HAS_ONE, ‘Location’, ‘id’) if used in the Profile Model this try to make a relation from Profile and Location based on Profile.id that must be a FK for another key in Location. You have to add this in the DB schema of the tables or you have to use row sql :)

Take a look at http://www.yiiframew…de/database.arr

Hm, I'm confused a bit.

I've successfully solved my problem with the next relation in profile model (thanks to StErMi and 20th time read of manual) : 'location' => array(self::BELONGS_TO, 'Location', 'country_id'),

But strange thing happened. I had my old relation, than I changed my relation to new one, refreshed the page - nothing changed. After that I changed relation name to location1 and that did the trick. Is there some kind of caching mechanism on relations?

maybe there is some cache on the server? I really don’t know :)