Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » DataObject as Page sorting by categories

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 724 Views
  • Bereusei
    Avatar
    Community Member
    94 Posts

    DataObject as Page sorting by categories Link to this post

    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.

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: DataObject as Page sorting by categories Link to this post

    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

  • Bereusei
    Avatar
    Community Member
    94 Posts

    Re: DataObject as Page sorting by categories Link to this post

    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.

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: DataObject as Page sorting by categories Link to this post

    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

    724 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.