Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Sort products

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

Page: 1 2
Go to End
Author Topic: 2193 Views
  • JMagnusson
    Avatar
    Community Member
    29 Posts

    Sort products Link to this post

    How can I sort my products (data objects) when the are displayed on the category pages? I have followed a tutorial at ssbits.com, http://www.ssbits.com/tutorials/2010/dataobjects-as-pages-part-2-using-model-admin-and-url-segments-to-create-a-product-catalogue.

    This is the code from the tutorial which I think is is there to make it:
    class CategoryPage_Controller extends Page_Controller
    {
       //Return the list of products for this category
    public function getProductsList()
       {
       return $this->Products(Null, 'Price ASC');
       }
    ...

    I want them sort by Title.

    Thanks,
    Johan

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Sort products Link to this post

    Substitute 'Price ASC' by 'Title ASC'?

  • JMagnusson
    Avatar
    Community Member
    29 Posts

    Re: Sort products Link to this post

    Unfortunately not. And the function doesnt work in the tutorial live preview either... Thats the problem with tutorials, when you dont understand all in them..

    /Johan

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Sort products Link to this post

    Yeah, well...

    return $this->Products(Null, 'Title');

    should return a DataObjectSet of all Products sorted on Title. I've not followed this tut, but I've tested this method, after I read about this 'undocumented bit of code' on ssbits, and it works for me...

  • JMagnusson
    Avatar
    Community Member
    29 Posts

    Re: Sort products Link to this post

    Wow, solved it. You are right, "return $this->Products(Null, 'Title');" is the way.
    But the problem was that the function was called "getProductList()" but the control in the .ss-file was named "Products".

    I got a little bit extra confused because it worked that way to (but without the right order).

    Thousands of thanks for your help.
    /Johan

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Sort products Link to this post

    O, right, things like that can make you pull out your hair

    In situations like this, where I'm never going to call the Products straight out, but always want the 'sorted' version, I use a function Products() in the controller, that does the sorting. So I can still use $Products in the in the template - they are the same products after all and the template doesn't have to know how I sorted them. Of course other people probably have different ideas...

  • liece
    Avatar
    Community Member
    9 Posts

    Re: Sort products Link to this post

    Hey all!

    I'm stuck at this same line in that tutorial, I tried replacing it with (like martimiz proposed):

    //Return the list of products for this category
       public function getProductsList()
       {
          return $this->Products(Null, 'Title);
       }

    but it didn't do anything here, how did it work for you?

    Thanks a lot

  • liece
    Avatar
    Community Member
    9 Posts

    Re: Sort products Link to this post

    Ok, resolved the problem by changing in the template file (thanks to martimiz and jmagnusson ) :

    <% control ProductsList %>

    and that worked perfectly. I even took it further by using in the template file:

    <% control getProductsList(3) %>

    and modifying the function in CategoryPage.php:

    public function getProductsList($num)
       {
          return $this->Products(Null, "Price,Title", Null, $num);

       }

    and it's perfect...

    Now I got a page that hold all my categories, calling <% control Children %> then <% control Products %> in the template, gives back the unsorted list of projects, but calling a function that sorts the products like the one above doesnt work at all.

    Has anyone find a solution to that (listing and sorting dataobjects that belong to children pages)?

    2193 Views
Page: 1 2
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.