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.

Data Model Questions /

DataObject::get -> sort by "Date" works, "Date DESC" doesn't


Reply


8 Posts   4114 Views

Avatar
oleze

Community Member, 65 Posts

7 May 2011 at 3:16am

I've added a DateField to my class "MediaReports" but

DataObject::get("MediaReport", "YEAR(Date) = YEAR(CURDATE())", 'Date DESC', "", "");

doesn't sort the reports descending. Ascending works perfectly. Any suggestion to do so (even a reverse function doesn't exist on DataObjectSet). Thank you.

Greatz
Oliver

Avatar
ryanwachtl

Community Member, 46 Posts

7 May 2011 at 6:04am

Edited: 07/05/2011 6:04am

Hi Oliver,

Does it work as expected with either

'Created DESC'

or

'LastEdited DESC'

You may want to post how you added the field to the $db array and to the CMS fields.

Avatar
oleze

Community Member, 65 Posts

7 May 2011 at 9:56am

So here's the piece of code:

<?php
class MediaReport extends File {
   static $db = array(
      ...
      'Date' => 'Date',
      ...
   );
.....

The DateField stores a user-entered date (publishing-date of the report by e.g. magazines or sth.). Normally, that should work.
You can find content like "2011-05-06" within the db-cell.

Avatar
ryanwachtl

Community Member, 46 Posts

7 May 2011 at 11:09am

Edited: 08/05/2011 10:29am

Everything should work, based on the information you provided so far. The issue is likely outside of the DataObject::get() and your database field. I would start looking at how you are calling the function and make sure it is indeed returning your filtered DataObject.

You can append

?showqueries=1

to the end of your URL to view the SQL SELECT query.

ORDER BY Date DESC

should be at the end of the SELECT statement if all is working properly

Avatar
oleze

Community Member, 65 Posts

8 May 2011 at 6:25am

?showqueries=1

seems not to work. That's what my function looks like:

function getCurrentReports() {
         $reports = DataObject::get("MediaReport", "YEAR(Date) = YEAR(CURDATE())", 'Date DESC', "", "");                              
         return $reports;
}

Avatar
oleze

Community Member, 65 Posts

8 May 2011 at 7:13am

You've got mail. Thanks.

Avatar
ryanwachtl

Community Member, 46 Posts

8 May 2011 at 7:45am

Edited: 08/05/2011 10:30am

So, What you've got going on currently is extending the File class and then assigning a has_one of more files and images to it. I think things would be a lot easier to work with if MediaReport extended DataObject and then create a has_one relationship for each type of file. You'll need to add Title to the $db array on MediaReport instead of referencing the PDF as Parent, it will be a has_one like the PDF_en file. Then you can easily query your MediaReport and sort by Date, once in the control loop in your template you will be able to access each of the files that the Object has.

In MediaReport.php

class MediaReport extends DataObject {
   static $db = array(
    'Title' => 'Text',
      'Title_en' => 'Text',
      'Magazin' => 'Text',
      'Date' => 'Date',
      'ExtLink' => 'Varchar(255)'
   );
   
   static $has_one = array(
      'Photo' => 'Image',
      'PDF' => 'File',
      'PDF_en' => 'File'
   );
   
...

public function getCMSFields_forPopup()
{   
   $datefield = new DateField('Date',_t('MEDIAREPORT.DATE','Date'));
   $datefield->setConfig('showcalendar', true);
   $datefield->setConfig('dateformat', 'dd.MM.YYYY');
   $filefield = new FileUploadField('PDF','PDF');
   $filefield->removeFolderSelection();
   $filefield2 = new FileUploadField('PDF_en','PDF_en');
   $filefield2->removeFolderSelection();
   $filefield->uploadFolder = 'mediareports/pdfs/';
   $filefield2->uploadFolder = 'mediareports/pdfs/';
   $imagefield = new ImageUploadField('Photo',_t('MEDIAREPORT.PHOTO','Photo'));
   $imagefield->removeFolderSelection();
   $imagefield->uploadFolder = 'mediareports/images/';
   return new FieldSet(
      new TextField('Title'),
      new TextField('Title_en'),
      $datefield,
      new TextField('Magazin'),
      new TextField('ExtLink'),
      $filefield,
      $filefield2,
      $imagefield
   );
}

In MediaReportsPage.php

class MediaReportsPage extends Page implements PermissionProvider {

...
      
      function getMediaReports_ThisYear() {
       $data = DataObject::get("MediaReport", "", 'Date DESC', "", "");                              
       return $data;
      }
   
...
   
}

In MediaReportsPage.ss

<% if MediaReports_ThisYear %>
   <ul>
         <% control MediaReports_ThisYear %>
          <h2>$title</h2>
          <% if PDF %>
          <% control PDF %>
          <p><a href="$link">$Name</a></p>
          <% end_control %>
          <% end_if %>
         <% end_control %>
   </ul>
<% end_if %>

Avatar
oleze

Community Member, 65 Posts

29 May 2011 at 2:49am

Hello Ryan,
i implemented the new code and everything works now. Thanks again for your help.

Oliver