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

How to group sorted pages by grandparent pages?


Go to End


2 Posts   1323 Views

Avatar
pinkp

Community Member, 182 Posts

27 May 2015 at 9:20am

Im trying to sort all my product pages across the site by their given date descending. But also group them by their grandparents title (as this is the product category) and finally if possible any that do not have a date (ProductReleaseDate) to be listed first before everything.

I have this function in my page controller that grabs all the product pages:

function ProductPages()
{
$productPages = ProductPage::get();
return $productPages ? $productPages : false;
}

Then in my template:

<% loop $ProductPages.Sort(ProductReleaseDate, DESC) %>
$Title
<% end_loop %>

This displays all my product pages titles in descending order by the ProductReleaseDate they have been given. They now need grouping..

I've been searching hard and can't find the right documentation or examples to get this right. Perhaps I need groupBy? I'm not sure if it needs to be in the controller or the template.. this may help but I need help. Many thanks.

http://docs.silverstripe.org/en/developer_guides/model/how_tos/grouping_dataobject_sets/

Avatar
Pyromanik

Community Member, 419 Posts

27 May 2015 at 8:40pm

Edited: 27/05/2015 8:42pm

sorry I didn't read all your post (well, thoroughly), so I might be off.

GroupedList::create($existingList)->GroupedBy('ParentID')
(GroupedList::groupBy simply adds a GROUP BY sql clause to the query, which is useless for what you want)

<% loop $GroupedListGetter %>$ParentID<% loop Children %>$Title, $Content, $Etc. ...

You could probably do something funky to get the Parent via the ParentID to get it's title, etc. Perhaps the ORM is smart enough to let you group by Parent.Title, I dunno. Fiddle with it :>