Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » DataObject::get by Year from a DateField?

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: 1030 Views
  • oleze
    Avatar
    Community Member
    64 Posts

    DataObject::get by Year from a DateField? Link to this post

    I'm trying to get all DataObjects of my custom Page-Type "MediaReports" with DataObject::get. All MediaReports have a DateField and I would like to output them for the current year (and seperate them from the older ones).

    I tried

    function getMediaReports_ThisYear() {
             $today = date('Y');
             return DataObject::get("MediaReport", "Year = '{$today}'", "Date DESC", "", "");
          }


    which works well with a NumericField called "Year". But I'd like to get this automatically from my DateField without declaring an extra NumericField. Can somebody help me?

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: DataObject::get by Year from a DateField? Link to this post

    Thanks to SQL functions, this is actually rather simple.

    function getMediaReports_ThisYear() {
    return DataObject::get("MediaReport", 'YEAR("DATE") = YEAR(CURDATE())', "Date DESC";
    }

  • oleze
    Avatar
    Community Member
    64 Posts

    Re: DataObject::get by Year from a DateField? Link to this post

    Thank you, works like a charm ;).

  • mierla
    Avatar
    Community Member
    16 Posts

    Re: DataObject::get by Year from a DateField? Link to this post

    I just wanted to add my thanks - I was able to use this construction to very easily retrieve a week's worth of events for a listing page - not just seven days from today, but a true week, including past days. Thank you!

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