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':
class Post extends CActiveRecord
{
public function relations()
{
return array(
'categories'=>array(self::MANY_MANY, 'Category',
'tbl_post_category(post_id, category_id)'),
);
}
}
class Category extends CActiveRecord
{
public function relations()
{
return array(
'Posts'=>array(self::MANY_MANY, 'Post',
'tbl_post_category(category_id, post_id)'),
);
}
}
So your code would look like:
class Owner extends CActiveRecord
{
public function relations()
{
return array(
'pets'=>array(self::MANY_MANY, 'Pet',
'tbl_post_category(pet_id, owner_id)'),
);
}
}
class Pet extends CActiveRecord
{
public function relations()
{
return array(
'owners'=>array(self::MANY_MANY, 'Post',
'tbl_post_category(owner_id, pet_id)'),
);
}
}
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:
public function primaryKey()
{
return 'owner_id';
}
Finally, you would retrive the information like this:
$owner = Owner::model()->findByPk((int)$id);
foreach($owner->pets as $pet)
{
print $pet->name;
}