I have Gallery and Photo model.
For Gallery preview, I want Gallery->photos to be random 5 Photos belonging to this given gallery. In other cases, Gallery->photos should be a list of all the photos belonging to this Gallery.
when calling
'data' => Gallery::model()->with('photos:preview')->findAll(),
it returns list of Galleries, and each Gallery->photos contain list of it’s photos.
The ‘order’ is proper (random), but the list contains ALL the photos belonging to the gallery, not only 5 of them. Simply, the limit is not applied
Here’s the Gallery::relations:
public function relations() {
return array(
'photos' => array(self::HAS_MANY, 'Photo', 'gallery_id'),
);
}
and Photo::scopes
public function scopes(){
return array(
'preview' => array('limit' => 5, 'order' => 'RAND()')
);
}