7913 Posts in 1355 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » Sort By Category
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
| Go to End | ||
| Author | Topic: | 3452 Views |
-
Re: Sort By Category

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.
-
Re: Sort By Category

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: categoryI 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?
-
Re: Sort By Category

2 July 2010 at 11:01pm
Ah, OK. Thanks for all your help, and looking forward to the next update to the module!
-
Re: Sort By Category

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");
-
Re: Sort By Category

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 %>
| 3452 Views | ||
| Go to Top |

