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.

Archive

Our old forums are still available as a read-only archive.

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

DataObject - Getting latest forum posts.


Go to End
Reply

10 Posts   2894 Views

Avatar
Josh

17 July 2008 at 2:43pm (Last edited: 17 July 2008 2:44pm), SilverStripe Developer, 65 Posts

Hey!

Don't quite understand the NOW() reference when calling data using DataObject.

How would I get the latest forum posts, starting with the most recent?

This is what I have (which doesn't return most recent)

function NewPosts() {
         $data = DataObject::get("Post", "", "LastEdited > NOW() - INTERVAL 48 HOUR", "", 8);
         return $data;
      }

Cheers!

Avatar
Willr

17 July 2008 at 4:28pm Forum Moderator, 5511 Posts

Looks like you have the 3rd and 2 argument for DataObject round the wrong way?.

Where you have NOW() that is the Sort argument, how you want the order of the set to come out. The 2 second argument is the Filter command. This is where you can write the SQL to filter the query.

NOW() is a MySQL command to output todays date. In your example what it is saying is Get all the posts where they were LastEdited in the Last 48 hours. (eg Get the Time Now and minus 48 hours).

If you need a query for latest forum posts see the ForumHolder.php - theres a RecentPosts() method you can call

Avatar
Josh

18 July 2008 at 10:49am (Last edited: 18 July 2008 10:59am), SilverStripe Developer, 65 Posts

Hi Will,

Thanks for explaining the NOW() function.

I have it working on my localhost, but on the live site the page just returns blank - though looks like it's trying to load for a few seconds. There is no errors showing up in the php error log.

Have you seen this happen on SS sites before - and do you have any idea where I should start looking for a solution?

Cheers,
Josh

Avatar
Sean

18 July 2008 at 11:23am Forum Moderator, 921 Posts

Hey there,

Try using Debug::send_errors_to('your@email.com'); in _config.php. This may send an email with the error you're having, even though it's only showing a blank page in the browser.

Sean

Avatar
Josh

18 July 2008 at 11:31am SilverStripe Developer, 65 Posts

Hi Sean,

Thanks for that. I have made sure the site is in Live environment, then added if(Director::isLive()) Debug::send_errors_to('myemail'); to _config.php and get no system emails about the error.

Thanks

Avatar
Sean

18 July 2008 at 12:09pm Forum Moderator, 921 Posts

I'm assuming that if you comment out the code snippet you posted earlier the live site works again?

Avatar
Josh

18 July 2008 at 12:11pm (Last edited: 18 July 2008 12:11pm), SilverStripe Developer, 65 Posts

Correct.

It's weird. I've never had an instance where code will work in a local environment, but not on a live server - that doesn't show errors anywhere.

Avatar
Sean

18 July 2008 at 12:40pm (Last edited: 18 July 2008 12:47pm), Forum Moderator, 921 Posts

I'm thinking that your LastEdited > NOW() should probably be the 2nd argument, instead of the 3rd. It seems you're trying to filter moreso than sort. Perhaps give that a try?

Alternatively, can you not do something like this?

DataObject::get('Post', '', 'LastEdited DESC', '', 8);

This would just get the latest 8 posts sorted by the LastEdited date (the latest 8).

Sean

Go to Top