Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » DataObject - Getting latest forum posts.

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

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

Page: 1 2
Go to End
Author Topic: 2841 Views
  • Josh
    Avatar
    SilverStripe Developer
    65 Posts

    DataObject - Getting latest forum posts. Link to this post

    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!

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: DataObject - Getting latest forum posts. Link to this post

    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

  • Josh
    Avatar
    SilverStripe Developer
    65 Posts

    Re: DataObject - Getting latest forum posts. Link to this post

    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

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: DataObject - Getting latest forum posts. Link to this post

    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

  • Josh
    Avatar
    SilverStripe Developer
    65 Posts

    Re: DataObject - Getting latest forum posts. Link to this post

    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

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: DataObject - Getting latest forum posts. Link to this post

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

  • Josh
    Avatar
    SilverStripe Developer
    65 Posts

    Re: DataObject - Getting latest forum posts. Link to this post

    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.

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: DataObject - Getting latest forum posts. Link to this post

    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

    2841 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.