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.

Data Model Questions /

Sort products


Go to End
Reply


10 Posts   2366 Views

Avatar
JMagnusson

Community Member, 29 Posts

12 January 2011 at 9:40pm

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

Avatar
martimiz

Forum Moderator, 1132 Posts

13 January 2011 at 1:20am

Substitute 'Price ASC' by 'Title ASC'?

Avatar
JMagnusson

Community Member, 29 Posts

13 January 2011 at 2:07am

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

Avatar
martimiz

Forum Moderator, 1132 Posts

13 January 2011 at 6:37am

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...

Avatar
JMagnusson

Community Member, 29 Posts

13 January 2011 at 6:54am

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

Avatar
martimiz

Forum Moderator, 1132 Posts

17 January 2011 at 6:36am

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...

Avatar
liece

Community Member, 9 Posts

6 May 2011 at 3:29am

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

Avatar
liece

Community Member, 9 Posts

6 May 2011 at 5:28am

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)?

Go to Top