17488 Posts in 4473 Topics by 1978 members
| Go to End | Next > | |
| Author | Topic: | 2543 Views |
-
DataObject - Getting latest forum posts.

17 July 2008 at 2:43pm Last edited: 17 July 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!
-
Re: DataObject - Getting latest forum 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
-
Re: DataObject - Getting latest forum posts.

18 July 2008 at 10:49am Last edited: 18 July 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 -
Re: DataObject - Getting latest forum 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
-
Re: DataObject - Getting latest forum 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
-
Re: DataObject - Getting latest forum 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?
-
Re: DataObject - Getting latest forum posts.

18 July 2008 at 12:11pm Last edited: 18 July 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.
-
Re: DataObject - Getting latest forum posts.

18 July 2008 at 12:40pm Last edited: 18 July 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
| 2543 Views | ||
| Go to Top | Next > |



