7913 Posts in 1355 Topics by 930 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, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 834 Views |
-
DataObject::get by Year from a DateField?

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? -
Re: DataObject::get by Year from a DateField?

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";
} -
Re: DataObject::get by Year from a DateField?

21 September 2010 at 8:40am
Thank you, works like a charm ;).
-
Re: DataObject::get by Year from a DateField?

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!
| 834 Views | ||
|
Page:
1
|
Go to Top |
