Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Sorting

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

Page: 1
Go to End
Author Topic: 1231 Views
  • Capt. Morgan
    Community Member
    30 Posts

    Sorting Link to this post

    I have a an AttributeValue DataObject, with has_one relation to the Attribute it is a value of. When listing the attribute values I want them sorted by a "Weight" parameter set in the Attribute. So I have a method in the controller for my class applying the attributes named SortedAttributeValues(). But I can not get it working. Can ofc make my own sorting algorithm but would rather figure out how to use the built in.


    // Turned out the sorting method does a $obj->$column when comparing.

    I'm very grateful if someone has some ideas to throw my way.

    EDIT: Simplified the problem.

  • Sam
    679 Posts

    Re: Sorting Link to this post

    The ProductAttributeValues() method can take, filter, sort, join, and limit arguments.

    See http://api.silverstripe.com/sapphire/model/DataObject.html#getComponents - the first argument to that method is automatically set to the string "ProductAttributeValues".

    In other words

    calling $this->ProductAttributeValues($f, $s, $j, $l);

    internally calls $this->getComponents("ProductAttributeValues", $f, $s, $j, $l);

  • Capt. Morgan
    Community Member
    30 Posts

    Re: Sorting Link to this post

    Thanks a lot Sam, works perfectly. I just had to type the join for ProductAttribute.

    function SortedProductAttributeValues()
       return $this->ProductAttributeValues('', 'ProductAttribute.Weight', 'INNER JOIN ProductAttribute ON ProductAttributeValue.ProductAttributeID = ProductAttribute.ID');

    Of course it would be even nice to have it sorted by $default_sort, but I guess it's hard to get that working with the sort value in a related table?

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.