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   335 Views

Avatar
Optic Blaze

20 January 2014 at 9:05pm Community Member, 162 Posts

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

20 January 2014 at 10:24pm Forum Moderator, 474 Posts

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

Avatar
Optic Blaze

20 January 2014 at 10:49pm Community Member, 162 Posts

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

20 January 2014 at 10:52pm Forum Moderator, 474 Posts

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

Avatar
mspacemedia

21 January 2014 at 3:07am Community Member, 7 Posts

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

Avatar
Optic Blaze

21 January 2014 at 6:51pm Community Member, 162 Posts

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