Jump to:

3373 Posts in 998 Topics by 712 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: 443 Views
  • cSGermany
    Avatar
    Community Member
    37 Posts

    get Dataobjects from Children - SilverStripe 3.1 Link to this post

    Hi,

    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

    cSGermany

  • zenmonkey
    Avatar
    Community Member
    524 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'
    ))->sort('RAND()')->limit($num);
    }

    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 %>
    $Visual.Setwidth(300)
    <% end_loop %>
    <% end_loop %>

    443 Views
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.