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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Sort By Category


Go to End


22 Posts   7823 Views

Avatar
DanStephenson

Community Member, 116 Posts

12 June 2009 at 4:36am

I am wondering if there is a way I can auto-sort my Data Objects based on category on the font-facing part of my site. So using my dropdown for category when the user ads an object, I'd like it to be put into the appropriate "bucket" on the Resources page template.

What would be the best way to achieve this?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

12 June 2009 at 4:57am

Edited: 12/06/2009 4:57am

Do you mean sort by category? Or filter by category? There's a difference. Either one is quite easy, though.

Avatar
DanStephenson

Community Member, 116 Posts

12 June 2009 at 5:26am

Edited: 12/06/2009 7:08am

I want to have them sorted I believe, so that my template looks like this

CATEGORY 1
Resource 1 Title
Resource 2 Title
Resource 3 Title

CATEGORY 2
Resource 1 Title
Resource 2 Title
Resource 3 Title

CATEGORY 3
Resource 1 Title
Resource 2 Title
Resource 3 Title

Avatar
UncleCheese

Forum Moderator, 4102 Posts

12 June 2009 at 5:57am

Edited: 12/06/2009 5:59am

something like this oughta work:

public function Categories()
{
$cats = new DataObjectSet();
foreach(singleton($this->class)->dbObject('Category')->enumValues() as $category) {
$cats->push(new ArrayData(array(
'Title' => $category, 
'Resources' => $this->Resources("Category = '$category'")
)));
}
return $cats;
}

<% control Categories %>
<h3>$Title</h3>
<% control Resources %>
...
<% end_control %>
<% end_control %>

Avatar
DanStephenson

Community Member, 116 Posts

12 June 2009 at 7:26am

Edited: 12/06/2009 7:27am

I was looking at something like that as well before I posted. I put that function into the ResourcePage Controller, but then when I add the code to my template

<% control Categories %>
<h3>$Title</h3>
<% control Resources %>
<a href="$Attachment.Link">$Attachment.Name &gt; $Description</a>
<% end_control %>
<% end_control %>

The template doesn't render. I just get a white page.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

12 June 2009 at 8:12am

That's a PHP error. Crank up your error reporting and find out what's going on.

Avatar
DanStephenson

Community Member, 116 Posts

12 June 2009 at 8:22am

UncleCheese, the error is on this line:

foreach(singleton($this->class)->dbObject('Category')->enumValues() as $category) {

Any ideas?

Avatar
DanStephenson

Community Member, 116 Posts

12 June 2009 at 8:41am

I got it working. I replaced "$this->class" to Resources and it worked fine.

One more question - I inserted a filter on the DOM in the admin section, so I can allow the admins to filter the categories if they wanted. However, it seems when I filter by category and then re-order, it doesn't apply (I can only reorder using no filter). I want the user to be able to filter by category on the backend and sort the resources, and for that order to be applied on the templates.

Any easy way to go about this?

Go to Top