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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Set Sort Order when creating a new DataObject


Go to End


13 Posts   11864 Views

Avatar
UncleCheese

Forum Moderator, 4102 Posts

7 August 2009 at 9:58am

Do an SVN update, and try adding in your config:

SortableDataObject::set_sort_dir("DESC");

Avatar
lawless

Community Member, 33 Posts

11 August 2009 at 9:26am

What's the correct argument to place my sort order if I'm using an IDOM?

I played around with it the other night with no luck. I'm trying to understand the whole DOM a bit more.

I don't have SVN setup so it's a bit of a hassle to update DEV then push out to PROD and honestly, I'm a bit reluctant to do it only because I'm new to SS and have spent more time debugging than I have coding.

I'm loving the DOM so far now that I've got a grasp of it, but is there some solid documentation for it anywhere?

So far, it's been a process of trial and error and searching the forums for solutions.

Avatar
WebSpilka

Community Member, 89 Posts

16 October 2009 at 11:33pm

Edited: 16/10/2009 11:36pm

UncleCheese
I download
http://carlinowebdesign.svn.beanstalkapp.com/modules/trunk/dataobject_manager/code/SortableDataObject.php

and
SortableDataObject::set_sort_dir("DESC"); - set sort for FrontEnd, but it not set order for Admin zone
How can I make so that when add a new item it is assigned SortOrder=0, and the rest was appropriated by the SortOrder +1.

Or what would be in the admin area of sorting was also SortOrder DESC

Avatar
UncleCheese

Forum Moderator, 4102 Posts

17 October 2009 at 1:15am

Did you download the file directly from your browser? You shouldn't be doing that. You need to checkout the entire package with SVN.

SortableDataObject works in conjunction with DataObjectManager, and works like this:

You register a class with SortableDataObject::add_sortable_class("MyClass")

This does the following:

1) Forces new objects to get a SortOrder value of (total records) + 1;
2) Allows drag and drop ordering in DOM
3) Forces default sort order of SortOrder (ASC/DESC) on the frontend.

Avatar
JonShutt

Community Member, 244 Posts

19 March 2012 at 2:07pm

Seems like these issue were never really fixed...

The problem is that the DataObjectManager can't do 'descending' columns, (clicking the titles gets a descending arrow but the results always count up) - or at least it can't when the list is sortable

So we have go about it differently. Keep the SortOrder counting up, but add your new item in at the start of the list, not the end.

This has to be done via onAfterWrite.
Note, I'm not using any write() commands in the function, becuase that invokes the 'onAfterWrite' function again, causing a loop.

Basically, we loop through all the items, increase their sort order by one, and set our new item to '0' to sit at the start of the list.

private $firstWrite = false;

function onBeforeWrite() {
parent::onBeforeWrite();
if (!$this->ID) $this->firstWrite = true;
}

function onAfterWrite() {

if ($this->firstWrite) {
$ProductPageID = $this->ProductPageID;
if ($records = DataObject::get('Item', "`ProductPageID` = '$ProductPageID' ", 'SortOrder ASC')) {

foreach ($records as $record) {
$origSort = $record->SortOrder;
if ($origSort == '') { $origSort = 0;}
$NewSortOrder = $record->SortOrder + 1;
$itemID = $record->ID;
//$record->write();
mysql_query("UPDATE `Item` SET `SortOrder`='$NewSortOrder' WHERE `ID`='$itemID' ");
}
}

$itemID = $this->ID;
mysql_query("UPDATE `Item` SET `SortOrder`='0' WHERE `ID`='$itemID' ");

}

return parent::onAfterWrite();
}

Go to Top