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

DataObject as Page sorting by categories


Go to End


4 Posts   1387 Views

Avatar
Bereusei

Community Member, 96 Posts

11 April 2012 at 9:33pm

Edited: 11/04/2012 9:48pm

He guys,

I´ve made an product catalogue following this tutorial: http://www.ssbits.com/tutorials/2010/dataobjects-as-pages-part-2-using-model-admin-and-url-segments-to-create-a-product-catalogue/

Now I have an Page where all products from all categories are listed. My problem is, I can´t figure out how to sort my products on the CategoryPage by categories and then by the title of the product, so that the products from one category are standing together.
I try to modify the getProductsList-function like this:

public function getProductsList()
{
return $this->Products(Null, 'Category.Title ASC, Title ASC');
}

I can´t figure out, how to call the category from the products.

Does anyone have a solution? I hope the problem is clear.

Avatar
zenmonkey

Community Member, 545 Posts

12 April 2012 at 2:14am

Are you using the DataObjectAsPage Module? It simplifies some of the work.

But if you're already looping though your products in the template you can do the sort right in the template by using the built in GroupedBy.

<% control Products.GroupedBy(Category)  %>
$Title
<% control Children %>
..product info..
<% end_control %>
<% end_control %>

and I'm pretty sure you can <% control Children.SortBy() %> in the template as well

Avatar
Bereusei

Community Member, 96 Posts

12 April 2012 at 8:03am

Thanks for reply. No, I don´t use the module. Start working on the project before the module came out.
I followed the tutorial and used that code.

GroupBy(Category) and SortBy(Category) doesn´t work:
<% control Products.GroupBy(Category) %>
<div class="produktthumb">
<a href="$Link">
<h2>$Title</h2>
<span class="price">$Price</span>
<div class="clear"></div>
$Image.CroppedImage(250,175)
</a>
</div>
<% end_control %>

<% control Products.GroupBy(Category) %> and <% control Products.SortBy(Category) %> only returns nothing.
<% control Products.GroupedBy(CategoryPage) %> and <% control Products.GroupedBy(Categories.ID) %> returns only one product.

Avatar
zenmonkey

Community Member, 545 Posts

12 April 2012 at 8:13am

You need a <% control Children %> control inside your GroupBy. The function creates a new DataObjectSet. You can use $Debug inside a control to help you determine what available to you