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

Using Groupby in SS Template


Go to End


6 Posts   1767 Views

Avatar
Optic Blaze

Community Member, 190 Posts

20 January 2014 at 9:05pm

Hi there i have the following object:

// JobSchedule.php
class JobSchedule extends DataObject {

public static $db = array(
'JobDate'=>'Date',
'Category'=>'Enum("Leak,Sanitary,Blocked drain")',
'Description'=>'Text',
'Status'=>'Enum("Active,Complete,Cancelled")',
'Start'=>'Time',
'End'=>'Time',
);
}

// Calendar.php

public function PlumberResults() {
$jobs = JobSchedule::get()->groupBy('Category');
return $jobs;

}

When i use this i get the following error: [User Error] Uncaught Exception: Object->__call(): the method 'groupby' does not exist on 'DataList'

How do i go about grouping objects?

Avatar
(deleted)

Community Member, 473 Posts

20 January 2014 at 10:24pm

You now need to wrap your list in a GroupedList. So, return GroupedList::create(JobSchedule::get())->groupBy('Category');

Avatar
Optic Blaze

Community Member, 190 Posts

20 January 2014 at 10:49pm

Thanks for the quick reply:

public function PlumberResults() {
$jobs = GroupedList::create(JobSchedule::get()->groupBy('Category'));
return $jobs;
}

Still returns ----- the method 'groupby' does not exist on 'DataList'

Avatar
(deleted)

Community Member, 473 Posts

20 January 2014 at 10:52pm

That's because you're still calling it on the DataList, not on the GroupedList. groupedBy() is a method on GroupedList().

Avatar
mspacemedia

Community Member, 12 Posts

21 January 2014 at 3:07am

Optic, As a hint to simon_w's answer you appear to be missing a bracket in your code ;)

Avatar
Optic Blaze

Community Member, 190 Posts

21 January 2014 at 6:51pm

Thanks guys...i got it to work now:

public function Grouptest() {
$group = GroupedList::create(JobSchedule::get())
->groupedBy('Category');
return $group;
}

And in my template i called:
<% loop $Grouptest %>
<% loop $Children %>
$Category - $ID - $Type <br>
<% end_loop %>
<% end_loop %>

THE FOLLOWING WORKED AS WELL

public function Grouptest() {
$group = GroupedList::create(JobSchedule::get());
return $group;
}

AND IN TEMPLATE
<% loop $Grouptest.GroupedBy(Category) %>
<% loop $Children %>
$Category - $ID - $Type <br>
<% end_loop %>
<% end_loop %>