Model Relations without PK

hello, what i have to do if i need to make relations with tables not by PK, but some columns of tables?

for example

house(id,switch,port)

lans(id,switch,port,vlan_id)

vlan(id,name)

house.switch=lans.switch, house.port=lans.port, vlan.id=lans.vlan_id

in all table PK - id, and i can’t just add column vlan_id in house

i need to make relations between house and vlan

could you help me plz? =)

ps: sorry for my english

I don’t think it’s possible to let the AR system handle that automatically. A simple solution could be to add a getVlan() method to your house model (which gets the lan by attributes + with vlan, after which you can return lan->vlan). However, if you have pages with a lot of houses on for which you need the vlan displayed, it could result it a big number of queries.

If you do want everything to happen automatically I’d suggest remodeling your database by either

  1. adding a lan_id to the house table

  2. creating a new table location(id,switch,port) and replace the switch & port columns in house and lans by a location_id (this would be my preferred method, since it’s more normalized)

cause can’t remodel the structure thought about method too

but it’s not cool lol. thx

I understand your case. You must to have a FOREIGN KEY mapped to a UNIQUE KEY. However, at least I can remember, Yii doesn’t handle it (The core just maintains PK and FK references).

I don’t know exactly how difficult could it be to implement it, but it is, for sure, a thing to think about, since you can have FK ==> UK.