Jump to:

7937 Posts in 1537 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Setting sort order in ImageDataObjectManager inside the control loop?

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1940 Views
  • lawless
    Avatar
    Community Member
    33 Posts

    Setting sort order in ImageDataObjectManager inside the control loop? Link to this post

    I'm trying to set the SQL query ORDER BY to DESC on my control loop for the IDOM.

    I've got r 301 and added this to the _config file in the dataobject_manager folder:

    SortableDataObject::set_sort_dir("DESC");

    My images are still sorted in ASC order. Here is the SQL query:

    SELECT `Surfboard`.*, `Surfboard`.ID, if(`Surfboard`.ClassName,`Surfboard`.ClassName,'Surfboard') AS RecordClassName FROM `Surfboard` WHERE (SurfboardPageID = '6')

    And the query for each individual ID within that query:

    SELECT `File`.*, `File`.ID, if(`File`.ClassName,`File`.ClassName,'File') AS RecordClassName FROM `File` WHERE (`File`.`ID` = 46) ORDER BY SortOrder DESC LIMIT 1

    It seems like I'd want to set the SortOrder DESC in the first query above, not the second one, as the second query has the LIMIT 1 so a sort order on a single item query is useless. It's the only item so there is nothing to order.

    Whereas if the SortOrder DESC was on the first query that pulled all the image IDs, it would sort them all in the preferred manner set in the _config file.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Setting sort order in ImageDataObjectManager inside the control loop? Link to this post

    I assume you have added SortableDataObject::add_sortable_class("Surfboard"); somewhere?

    The thing is, unless you opt out of allowing DOM to handle the Files & Images section of the CMS, File gets automatically added to SortableDataObject to allow drag and drop in AssetAdmin. Therefore, every query for a File is going to append an ORDER BY SortOrder, regardless of the limit.

    That your Surfboard query is not showing an ORDER BY clause is a little suspect to me. Are you getting the "allow drag and drop" checkbox on your ImageDOM? Something isn't right there. Try adding the add_sortable_class("Surfboard") to your /mysite/_config.php instead. Better practice, anyway, since then your config doesn't get overwritten with every update.

  • lawless
    Avatar
    Community Member
    33 Posts

    Re: Setting sort order in ImageDataObjectManager inside the control loop? Link to this post

    "I assume you have added SortableDataObject::add_sortable_class("Surfboard"); somewhere?"

    No, I didn't. I added this to my mysite/_config file

    // Set sort order for SQL queries
    SortableDataObject::set_sort_dir("DESC");
    SortableDataObject::add_sortable_class("Surfboard");

    I just updated to the r315 and noticed that added a sort order field to my Surfboards table. I had to manually number all the existing entries, but it looks like new entries get a next number added by default. This gets everything to sort in the order that I want it displayed.

    I was getting the "Allow drag and drop" checkbox. I was using a pretty old revision on my Production site though.

    I just updated my Production site and everything is looking good. Thanks for the help.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Setting sort order in ImageDataObjectManager inside the control loop? Link to this post

    You shouldn't have had to update the SortOrder manually. It should have done that after the first drag-and-drop.

  • lawless
    Avatar
    Community Member
    33 Posts

    Re: Setting sort order in ImageDataObjectManager inside the control loop? Link to this post

    I didn't turn on the drag-and-drop ordering. I didn't want the guy who updates the CMS to have to re-order the boards after every upload.

    From what it sounds like you're saying though, is if I had turned it on and drag-and-dropped one image it would have added the sort order to them all. Good to know, but I was trying to get everything working without the drag-drop reording. I want the upload process to be as quick and easy as possible and handle all the sorting automatically on the back-end.

    Just got an email from the guy who updates it and he was stoked. Said it worked flawlessly.

    I'll play with the drag-and-drop on my DEV site. I haven't set the sort orders on there, so I can see how it handles it.

    Thanks again for the help and the great module.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Setting sort order in ImageDataObjectManager inside the control loop? Link to this post

    Hmm.. Okay, glad it's working. But just a tip.. If you're not going to use drag-and-drop reordering, then you don't need to use SortableDataObject. You just need to add a sort clause to your ImageDOM for "Created DESC"

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