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.

Archive /

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

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

DataObject - Getting latest forum posts.


Go to End


10 Posts   3621 Views

Avatar
Josh

SilverStripe Developer, 65 Posts

17 July 2008 at 2:43pm

Edited: 17/07/2008 2:44pm

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

Forum Moderator, 5523 Posts

17 July 2008 at 4:28pm

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

SilverStripe Developer, 65 Posts

18 July 2008 at 10:49am

Edited: 18/07/2008 10:59am

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

Forum Moderator, 922 Posts

18 July 2008 at 11:23am

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

SilverStripe Developer, 65 Posts

18 July 2008 at 11:31am

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

Forum Moderator, 922 Posts

18 July 2008 at 12:09pm

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

Avatar
Josh

SilverStripe Developer, 65 Posts

18 July 2008 at 12:11pm

Edited: 18/07/2008 12:11pm

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

Forum Moderator, 922 Posts

18 July 2008 at 12:40pm

Edited: 18/07/2008 12:47pm

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