CActiveDataProvider for related data (HAS_MANY)


let’s assume I have Gallery model which has following relation:

'photos' => array(self::HAS_MANY, 'Photo', 'gallery_id');

now, when displaying single gallery page I’d like to retrieve CActiveDataProvider for $gallery->photos, so I can pass it to CListView.

Is that possible anyhow?

Just an example, customize it as you like:

$photos=Photo::model()->findAll('gallery_id=:gid', array(':gid'=>$data->gallery_id));



    foreach($photos as $attribute)


        echo $attribute->your_attribute;




Sounds like a simple task that is very similar to the one shown in the blog tutorial. there, there are blog post and you want to show the post related comments. Check out how its done here:

guys, I know the way you showed, it’s obvious and trivial.

What I’m looking for is how to feed CActiveDataProvider with data from “related” field.

What I mean is:

  1. I define relation

  2. I create CActiveDataProvider for that

writing findAll(‘galler_id = :gid’, …) is in fact redefinition of relation, which is against DRY [Don’t Repeat Yourself] ;)