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 | Next > | |
| Author | Topic: | 3459 Views |
-
Sort By Category

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

12 June 2009 at 4:57am Last edited: 12 June 2009 4:57am
Do you mean sort by category? Or filter by category? There's a difference. Either one is quite easy, though.
-
Re: Sort By Category

12 June 2009 at 5:26am Last edited: 12 June 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 TitleCATEGORY 2
Resource 1 Title
Resource 2 Title
Resource 3 TitleCATEGORY 3
Resource 1 Title
Resource 2 Title
Resource 3 Title -
Re: Sort By Category

12 June 2009 at 5:57am Last edited: 12 June 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 %> -
Re: Sort By Category

12 June 2009 at 7:26am Last edited: 12 June 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 > $Description</a>
<% end_control %>
<% end_control %>The template doesn't render. I just get a white page.
-
Re: Sort By Category

12 June 2009 at 8:12am
That's a PHP error. Crank up your error reporting and find out what's going on.
-
Re: Sort By Category

12 June 2009 at 8:22am
UncleCheese, the error is on this line:
foreach(singleton($this->class)->dbObject('Category')->enumValues() as $category) {
Any ideas?
-
Re: Sort By Category

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?
| 3459 Views | ||
| Go to Top | Next > |
