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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Selecting next event in controller?


Go to End


3 Posts   725 Views

Avatar
Apc204

Community Member, 12 Posts

14 January 2015 at 1:15am

I have a section of my site called 'Events' where I have child pages called 'Article' with a Date field. I am trying to set up a "Next Event" section on the homepage which will pick out the next event by date (there may be 10 events coming up but I need to select the closest one).

I have been trying to write a function in the Homepage Controller to pick out the next event but I cannot work out how to select properties of the child pages such as Title or Date. The code I have so far is

 class Homepage_Controller extends Page_Controller {
    public function nextEvent(){
        $children = $this->ChildrenOf('Events');
        $event = "";

       //Code to select next event

        return $event;
    }
} 

I've been searching around for a long time but cannot find a similar example anywhere, could anyone point me in the right direction?

Many thanks,
Adam.

Avatar
martimiz

Forum Moderator, 1391 Posts

14 January 2015 at 2:55am

you should be able to filter $children directly to find the next event. Supposing you are at v3.1.x, something like

$next = $this->ChildrenOf('Events')
        ->filter(array('Date:GreaterThan' => $currentDate))
        ->sort('Date')
        ->first();

Avatar
Apc204

Community Member, 12 Posts

14 January 2015 at 4:05am

Thanks martimiz! I got that to work :)