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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

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

Sorting


Go to End


3 Posts   1635 Views

Avatar
Capt. Morgan

Community Member, 30 Posts

8 March 2009 at 5:58am

Edited: 08/03/2009 7:44am

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.

$this->ProductAttributeValues()->sort('ProductAttribute->Weight');

// Turned out the sorting method does a $obj->$column when comparing.
//$this->ProductAttributeValues()->sort('ProductAttribute.Weight');

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

EDIT: Simplified the problem.

Avatar
Sam

Administrator, 690 Posts

9 March 2009 at 10:07am

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

Avatar
Capt. Morgan

Community Member, 30 Posts

10 March 2009 at 7:48am

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?