AR Many-to-Many Relations

Hi,

I am trying to use the AR relations on a many-to-many relationship without much luck

I have 3 tables that I am trying to join, the tables are as follows:

reptile - primary key: reptile_id

reptile_owner_link - reptile_id and owner_id

owner - primary key: owner_id

I currently having the following in my Owner Model:


public function relations()

{


	return array(

			'reptile'=>array(self::MANY_MANY, 'reptile',

					'reptile_owner_link(reptile_id,owner_id)'),

	); 

}

And this is my Reptile Model:


public function relations()

{

	return array(

			'owner'=>array(self::MANY_MANY, 'owner',

				'reptile_owner_link(reptile_id,owner_id)'),

		);

}

And in my controller i’m using


$reptile= Reptile::model()->findByPk(3);

I want to obtain a list of all the reptiles, and their owners. Currently it just returns the row in the reptile table.

Even if I change the key names to ones that don’t exist it still returns the data from reptile without throwing an error.

I am getting very confused!!

Thanks in advance.

Dave.

You are nearly there.




//Load reptile of interest

$reptile= Reptile::model()->findByPk(3);


//Find all related owners

print_r($reptile->owner);



If you want ALL reptiles and owners you need to do a findAll() query instead of findByPk which loads a single instance.

Curious about the data model though - do 2 people really co-own a lizard?