Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Using Groupby in SS Template

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

Page: 1
Go to End
Author Topic: 319 Views
  • Optic Blaze
    Avatar
    Community Member
    162 Posts

    Using Groupby in SS Template Link to this post

    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?

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Using Groupby in SS Template Link to this post

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

  • Optic Blaze
    Avatar
    Community Member
    162 Posts

    Re: Using Groupby in SS Template Link to this post

    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'

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Using Groupby in SS Template Link to this post

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

  • mspacemedia
    Avatar
    Community Member
    7 Posts

    Re: Using Groupby in SS Template Link to this post

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

  • Optic Blaze
    Avatar
    Community Member
    162 Posts

    Re: Using Groupby in SS Template Link to this post

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

    319 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.