I am looking at YII for the first day, and i’m having some problems trying to work out the relations between some tables.
my table structure is as follows:
Pets:
pet_id
pet_name
….
Pet_Owner_Link
pet_id
owner_id
Owner:
owner_id
owner_name
How would I go about getting all of the pets that belong to an owner? Really struggling to get my head around the AR relations at the moment.
Per this comment by DD.Jarod on the Yii AR documentation page: http://www.yiiframework.com/doc/guide/1.1/en/database.arr#c970
“If you declare a many to many relationship, the order of keys inside the jointable declaration must be ‘my_id, other_id’:
So your code would look like:
Your problem may be that your primary keys for Pet and Owner by default should be id (not pet_id / owner_id). Yii may be getting confused if you don’t clarify that your primary keys don’t match the default naming convention / aren’t setup as primary keys in the database. You can specify what your primary key is in a model like this:
Finally, you would retrive the information like this: