I have a db table where I store resources (server details) and another table that contain a list of operating systems.
My two models:
I have omitted some functions for briefness.
In the site controller, I have:
When I insert new resources, resources.op_system in database is saved ok. The listResources action however displays the server always with the same OS, the first in the list. Whatever value I select for a new resource, the listing of resources displays all resources with the same OS.
What am I doing wrong?
You just need to set your foreign key on the Resource BELONGS_TO relation correctly. It should be 'op_system' like below. Hope this helps.
OK, that worked! Thanks!
However, I don't understand why I should set op_system there and not the primary key of the op_systems db table. Could you please explain?
It's because it's a BELONGS_TO relation, so the system will already know which field in the other table to look for (
id by default, and in this case, more generally this is the primary key and could be overridden where necessary). So for a BELONGS_TO relationship you need to specify which field in the child table is used for the join, in this case
The resulting join condition will be something like which is what you want.
By providing the primary key of the op_systems table you are given yii no clues as to what to compare that with.
Thanks! That was mostly helpful!