Three table join - how best to do it?

Three tables: post, postimage, image. Postimage is a ‘join’ table between post and image as they are many to many.

post columns: post_id, post_topic_id, post_text

postimage columns: pi_post_id, pi_image_id

image columns: image_id, image_file

Post model has relation: ‘images’ => array(self::HAS_MANY, ‘PostImage’, ‘pi_post_id’),

so in the Topic controller I can get the current topic’s post_ids (from post table) and the image_ids (from postimage table) with…




$dataProvider=new CActiveDataProvider('Post', array( // all the posts for this topic

	'criteria'=>array(

	'condition'=>'post_topic_id='.intval($_GET['id']),

	'with'=>'images',

	));

but then how do I get the image_file fields for each post as well from the third table?

I’m going to attempt this using ajax in the view to pull the image_files where a post has them but wondered if there was a clean way of doing this in the controller?

You can go all the way with this relation.




'images' => array(self::MANY_MANY, 'Image', 'PostImage(pi_post_id, pi_image_id)'),



Does this make it any easier. I’m not sure exactly what you’re trying to accomplish. Why not just create image tags in your view?

Thanks for your answer. I can see it in the docs now as well. However it’s not pulling in the third table in the join for some reason…

edit: is working…

I’m aiming to have one or more uploaded images displayed alongside a post.