Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » get Dataobjects from Children - SilverStripe 3.1

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

Page: 1
Go to End
Author Topic: 588 Views
  • cSGermany
    Community Member
    37 Posts

    get Dataobjects from Children - SilverStripe 3.1 Link to this post


    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?

    GalleryHolder: http://sspaste.com/paste/show/525e4b9134940
    Gallery: http://sspaste.com/paste/show/525e4bb25f236
    VisualObject: http://sspaste.com/paste/show/525e4bd3cdfff

    thx in advance


  • zenmonkey
    Community Member
    528 Posts

    Re: get Dataobjects from Children - SilverStripe 3.1 Link to this post

    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 %>

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.