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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

get Dataobjects from Children - SilverStripe 3.1


Go to End


2 Posts   2555 Views

Avatar
cSGermany

Community Member, 37 Posts

16 October 2013 at 9:30pm

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

Avatar
zenmonkey

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