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.

Data Model Questions /

Using Groupby in SS Template


Reply


6 Posts   413 Views

Avatar
Optic Blaze

Community Member, 165 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
simon_w

Forum Moderator, 474 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, 165 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
simon_w

Forum Moderator, 474 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, 7 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, 165 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 %>