Jump to:

7938 Posts in 1540 Topics by 945 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Set Sort Order when creating a new DataObject

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2
Go to End
Author Topic: 8022 Views
  • UncleCheese
    Avatar
    4085 Posts

    Re: Set Sort Order when creating a new DataObject Link to this post

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

    SortableDataObject::set_sort_dir("DESC");

  • lawless
    Avatar
    Community Member
    33 Posts

    Re: Set Sort Order when creating a new DataObject Link to this post

    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.

  • fiord
    Avatar
    Community Member
    79 Posts

    Re: Set Sort Order when creating a new DataObject Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Set Sort Order when creating a new DataObject Link to this post

    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.

  • Futureweb
    Avatar
    Community Member
    232 Posts

    Re: Set Sort Order when creating a new DataObject Link to this post

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

    8022 Views
Page: 1 2
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.