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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

DataObject::get by Year from a DateField?


Go to End


4 Posts   2078 Views

Avatar
oleze

Community Member, 65 Posts

21 September 2010 at 6:34am

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?

Avatar
(deleted)

Community Member, 473 Posts

21 September 2010 at 7:23am

Thanks to SQL functions, this is actually rather simple.

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

Avatar
oleze

Community Member, 65 Posts

21 September 2010 at 8:40am

Thank you, works like a charm ;).

Avatar
mierla

Community Member, 16 Posts

27 January 2012 at 2:10am

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!