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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Sorting many_many relation in front end


Reply


5 Posts   1296 Views

Avatar
JoshuaLewis

Community Member, 76 Posts

16 October 2009 at 12:14am

Edited: 16/10/2009 12:15am

Like the title say I've got a many_many relation and when I show the objects in the front end they need to be sorted by product ID numbers that the client sets. But when I use <% control Products %> they are called up in the order they were created.

Is there away of achieving this that I haven't found or will I have to create a custom method that calls Dataobject::get().

Avatar
Hamish

Community Member, 712 Posts

16 October 2009 at 1:20pm

You can do this:

<% if Products %>
   $Products.sort(Price)
   <% control Products %>
      // content
   <% end_control %>
<% end_if %>

But due to an annoying limitation of SSViewer, you can't set the order (it is ascending by default). For anything more complicated you'll need your own method.

Avatar
JoshuaLewis

Community Member, 76 Posts

16 October 2009 at 3:17pm

Thank you, that should do just what I need.

Is there an entry in the documentation that covers this or other built in functions that can be applied in the template?

Avatar
dalesaurus

Community Member, 283 Posts

16 October 2009 at 3:49pm

BOOP - http://doc.silverstripe.org/doku.php?id=built-in-page-controls

Avatar
JoshuaLewis

Community Member, 76 Posts

17 October 2009 at 1:08am

Thanks dalesaurus, that is a good reference but there's nothing there about a sort() method. Just a reference to the Sort field in the Sitetree table of the database.

I've been experimenting and found that the method is coming from the DataObjectSet class which should help answer most of the questions I have. I expect that I'll be back if it doesn't.

Anyone know what other core classes I can expect to show up in the templates? I'm assuming their methods would be available in the same way.