Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » DataObject::get -> sort by "Date" works, "Date DESC" doesn't

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

Page: 1
Go to End
Author Topic: 3762 Views
  • oleze
    Avatar
    Community Member
    64 Posts

    DataObject::get -> sort by "Date" works, "Date DESC" doesn't Link to this post

    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

  • ryanwachtl
    Avatar
    Community Member
    46 Posts

    Re: DataObject::get -> sort by "Date" works, "Date DESC" doesn't Link to this post

    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.

  • oleze
    Avatar
    Community Member
    64 Posts

    Re: DataObject::get -> sort by "Date" works, "Date DESC" doesn't Link to this post

    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.

  • ryanwachtl
    Avatar
    Community Member
    46 Posts

    Re: DataObject::get -> sort by "Date" works, "Date DESC" doesn't Link to this post

    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

  • oleze
    Avatar
    Community Member
    64 Posts

    Re: DataObject::get -> sort by "Date" works, "Date DESC" doesn't Link to this post

    ?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;
    }

  • oleze
    Avatar
    Community Member
    64 Posts

    Re: DataObject::get -> sort by "Date" works, "Date DESC" doesn't Link to this post

    You've got mail. Thanks.

  • ryanwachtl
    Avatar
    Community Member
    46 Posts

    Re: DataObject::get -> sort by "Date" works, "Date DESC" doesn't Link to this post

    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 %>

  • oleze
    Avatar
    Community Member
    64 Posts

    Re: DataObject::get -> sort by "Date" works, "Date DESC" doesn't Link to this post

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

    Oliver

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