Jump to:

23001 Posts in 11854 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Sorting many_many relation in front end

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

Page: 1
Go to End
Author Topic: 1216 Views
  • JoshuaLewis
    Avatar
    Community Member
    76 Posts

    Sorting many_many relation in front end Link to this post

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

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Sorting many_many relation in front end Link to this post

    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.

  • JoshuaLewis
    Avatar
    Community Member
    76 Posts

    Re: Sorting many_many relation in front end Link to this post

    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?

  • dalesaurus
    Avatar
    Community Member
    283 Posts
  • JoshuaLewis
    Avatar
    Community Member
    76 Posts

    Re: Sorting many_many relation in front end Link to this post

    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.

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