Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

get Dataobjects from Children - SilverStripe 3.1

Go to End

2 Posts   2030 Views


Community Member, 37 Posts

16 October 2013 at 9:30pm


I've got a GalleryHolder with Gallery-Pages as children. Each Gallery-Page has a Dataobject(VisualObject) to store the images.

I managed it to get 3 random images from a GalleryPage on it's gallery page and 3 random images from all galleries on the GalleryHolder page.

But what I want are 3 random images for each gallery shown on the GalleryHolder page.

Here's my Code, can someone tell me how to do that?


thx in advance



Community Member, 545 Posts

20 October 2013 at 10:32am

If this is for the front end, it's really a tempalte issue. What you need to do is put the random image getter on the Gallery

public function RandomPreview($num=3) {
     return $this->GalleryImages()->filter(array(
         'Visibility' => 'true'

Best practices states that anyy function accesable on the template should be upper camel case. Also you don't need to do getChildren function unless you're overloading it some way. It's availabel on every page. So now on your GalleryHolder template you would do

<% loop Children %>
  <% loop RandomPreview %>
  <% end_loop %>
<% end_loop %>