Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Sort By Category

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2 3
Go to End
Author Topic: 3834 Views
  • Matty Balaam
    Avatar
    Community Member
    69 Posts

    Re: Sort By Category Link to this post

    Wow, thanks again. I had assumed there was probably a better way of doing it - but had no idea where to begin. After only just getting to grips with how the old bit of code worked, I'll study that and try and work out how it works too.

  • Matty Balaam
    Avatar
    Community Member
    69 Posts

    Re: Sort By Category Link to this post

    Can't work out how to get this leaner code to display properly in the template.

    <% control PublicationYears %>
    <h3>$Title</h3>
    <% control Publications %>
    <div class="Publication"><p>$Authors. ($Date.format(Y).) <a href="$Attachment.URL">$Name</a> $Description.</p>
    <p class ="category">Type of Publication: $Category.</p></div>
    <% end_control %>
    <% end_control %>


    Instead of:

    2010
    author. (2010.) publication name 4 description.
    Type of Publication: category
    author. (2010.) publication name 3 description.
    Type of Publication: category
    2009
    author. (2009.) publication name 2 description.
    Type of Publication: category
    author. (2009.) publication name 1 description.
    Type of Publication: category

    I get:

    publication 4
    . (.) Publications .
    Type of Publication:
    publication 3
    . (.) Publications .
    Type of Publication:
    publication 2
    . (.) Publications .
    Type of Publication:
    publication 1
    . (.) Publications .
    Type of Publication:

    Any ideas?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Sort By Category Link to this post

    Sorry, I misread your code. You had it right.

  • Matty Balaam
    Avatar
    Community Member
    69 Posts

    Re: Sort By Category Link to this post

    Ah, OK. Thanks for all your help, and looking forward to the next update to the module!

  • Matty Balaam
    Avatar
    Community Member
    69 Posts

    Re: Sort By Category Link to this post

    Just an update on this in case anyone ever needs it.

    Some 2011 publications were added to the page but were showing at the end, rather than the top. This was because I hadn't specified the order of the initial SQL query, this was easily fixed by adding 'order by Date desc' to the end.

    $result = DB::query("SELECT DISTINCT YEAR(Date) as Year FROM `Publication` order by Date desc");

  • Matty Balaam
    Avatar
    Community Member
    69 Posts

    Re: Sort By Category Link to this post

    Another follow up. I upgraded this to use SS3, so it's not actually in DOM anymore, but for showing the object here's a much more elegant solution, adpated from: http://doc.silverstripe.org/framework/en/howto/grouping-dataobjectsets

    class Publication extends DataObject
    {
    public function getYearCreated() {
    return date('Y', strtotime($this->Date));
    }
    }      

    class PublicationPage_Controller extends Page_Controller
    {
    public function getGroupedPublicationsByDate() {
    return GroupedList::create(Publication::get()->sort('Date desc'));
    }
    }

    templates/includes/Publication.ss

    <% loop getGroupedPublicationsByDate.GroupedBy(YearCreated) %>
    <h3>$YearCreated</h3>
    <% loop Children %>
    <a class="publication sheet" href="$Attachment.URL">
    <p>$Authors. ($Date.year) <strong>$Name</strong> $Description.</p>
    <p>Type of Publication: $Category.</p>
    </div>
    </a>
    <% end_loop %>
    </div>
    <% end_loop %>

    3834 Views
Page: 1 2 3
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.