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.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Set Sort Order when creating a new DataObject


Go to End
Reply

13 Posts   8112 Views

Avatar
Shawn Parr

3 July 2009 at 1:36am Community Member, 60 Posts

This may be more of a general DataModel question, but since I'm using DOM and the Sortable capability I'm posting here.

I have a page that uses DOM to manage some data. It is actually used for a news page, and the DataObjects are the news articles. The end users would like the ability to use the drag and drop reordering to change the order of the articles displayed on the page. Easy enough.

However they also want new articles to appear on top after being created. Usually the newest items go to the bottom of the list. Can someone point me in the right direction to figure out how to make the new DataObjects, when created, to go to the top of the list (hence the lowest sort order value)?

TIA!

Avatar
UncleCheese

3 July 2009 at 2:22am 4085 Posts

You could set a default sort.. I believe it's the 6th argument of DOM and 7th argument of FDOM.

Avatar
Shawn Parr

3 July 2009 at 2:59am Community Member, 60 Posts

Well, a default sort in this case doesn't seem to work, as it doesn't affect the order the objects are viewed in the DOM view in the CMS, and I'm already doing a custom function for displaying them on a page due to using the paging feature.

So for my controller/template (NewsPage.php):

function PagingArticles() {
         if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) {
            $_GET['start'] = 0;
         }
         
         $SQL_start = (int)$_GET['start'];
         $doSet = DataObject::get('NewsArticle', "`NewsPageID` = '{$this->ID}'", "SortOrder DESC", "", "$SQL_start, 10");
         return $doSet ? $doSet : false;
      
      }

What I'm going for is mimicking this behavior in the view in the CMS. I thought I'd be all clever, and add SortOrder to the table view (NewsPage.php):

$fields = parent::getCMSFields();
         $NewsManager = new DataObjectManager(
            $this,
            'Articles',
            'NewsArticle',
            array('SortOrder' => 'SortOrder', 'Date' => 'Date', 'Contact' => 'Contact', 'Heading' => 'Heading'),
            'getCMSFields_forPopup'
         );

And I was excited to find that it does indeed show a column with the sort ID's from the database, however clicking on the heading to sort with that doesn't work. Any other header does. So I'm not sure what is going on there.

Hopefully that gives you an idea of what I'm trying to do. Any further ideas?

I'll warn you I'm also having some trouble with the CustomSearchForm you posted about elsewhere, but I got more digging to do with that, and I'll post in that thread when I finally give up. :)

Avatar
Shawn Parr

3 July 2009 at 3:14am (Last edited: 3 July 2009 3:14am), Community Member, 60 Posts

A quick update, it appears the sort order field kind of works, however no matter if the arrow in the header is ASC or DESC it always displays the dataobjects ASC. Which unfortunately is the opposite of how I would like it to work in this instance.

Avatar
theAlien

14 July 2009 at 2:16am (Last edited: 14 July 2009 2:22am), Community Member, 131 Posts

Default sort order in DOM is Ascending on creationdate. To sort descending on Date, do this:

$fields = parent::getCMSFields();
$NewsManager = new DataObjectManager(
$this,
'Articles',
'NewsArticle',
array( 'SortOrder' => 'SortOrder', 'Date' => 'Date', 'Contact' => 'Contact', Heading' => 'Heading' ),
'getCMSFields_forPopup',
'', //this argument sets a filter
'Date DESC'
);

Instead of Date you also can use the hidden column Created, that would give: 'Created DESC',
or sort on the hidden column LastEdited ('LastEdited DESC').

Avatar
Shawn Parr

6 August 2009 at 4:02am Community Member, 60 Posts

Sorry for the huge delay getting back to this. I was looking at it again today. I'm sorry I misunderstood before, as I totally missed that you could set a sort order when calling DataObjectManager.

However it still doesn't seem to work when SortOrder is the field you are sorting by. It will sort by SortOrder but the ASC DESC is always ASC. I need to have DESC. I'm not sure if this is some weird issue with the datamodel, DOM, or something in my installs. I have multiple installs that all seem to do this though.

Any ideas/help would be greatly appreciated.

Here is my current code:

public function getCMSFields() {
      
         $fields = parent::getCMSFields();
         $NewsManager = new DataObjectManager(
            $this,
            'Articles',
            'NewsArticle',
            array('SortOrder' => 'SortOrder', 'Date' => 'Date', 'Contact' => 'Contact', 'Heading' => 'Heading'),
            'getCMSFields_forPopup',
            '',
            'SortOrder DESC',
            ''
         );

Avatar
lawless

7 August 2009 at 9:25am Community Member, 33 Posts

Shawn, try 'Created DESC', in place of 'SortOrder DESC',

That should at least put the newest news articles at the top.

I'm having the same sort issues with an ImageDataObjectManager, so I'm hoping that will solve my problem and put the newest images at the top of the display instead of at the bottom.

UncleCheeze mentioned the sort order is the 6th argument in DOM and you've got it in the 7th argument it looks like. Might try:

public function getCMSFields() {

$fields = parent::getCMSFields();
$NewsManager = new DataObjectManager(
$this,
'Articles',
'NewsArticle',
array('SortOrder' => 'SortOrder', 'Date' => 'Date', 'Contact' => 'Contact', 'Heading' => 'Heading'),
'getCMSFields_forPopup',
'SortOrder DESC'
);

And see if that works.

Avatar
Shawn Parr

7 August 2009 at 9:35am Community Member, 60 Posts

When I made it the 6th argument it failed:

[06-Aug-2009 04:31:27] Error at sapphire/core/model/MySQLDatabase.php line 400: Couldn't run query:
SELECT `NewsArticle`.*, `NewsArticle`.ID, if(`NewsArticle`.ClassName,`NewsArticle`.ClassName,'NewsArticle') AS RecordClassName
FROM `NewsArticle`
WHERE ((SortOrder DESC) AND (`NewsPageID` = '1'))
ORDER BY SortOrder ASC
LIMIT 0,10

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC) AND (`NewsPageID` = '1')) ORDER BY SortOrder ASC LIMIT 0,10' at line 1 (http://astest/shawn/news/admin/getitem?ID=1&ajax=1)

I'll have to play with the Created a bit more. On my test setup it isn't working as I adjusted all my entries with sort orders. If I have time tomorrow I'll suck in the database from the real site to see if it behaves any differently.

Go to Top