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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Sort By Category


Go to End
Reply


22 Posts   4349 Views

Avatar
Matty Balaam

Community Member, 69 Posts

1 July 2010 at 5:50am

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.

Avatar
Matty Balaam

Community Member, 69 Posts

1 July 2010 at 9:23pm

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?

Avatar
UncleCheese

Forum Moderator, 4096 Posts

2 July 2010 at 3:50am

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

Avatar
Matty Balaam

Community Member, 69 Posts

2 July 2010 at 11:01pm

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

Avatar
Matty Balaam

Community Member, 69 Posts

12 April 2011 at 7:58pm

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");

Avatar
Matty Balaam

Community Member, 69 Posts

25 September 2012 at 12:36am

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

Go to Top