Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Bumbling with LatestNews DataObject [solved]

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

Page: 1
Go to End
Author Topic: 1220 Views
  • Parradoxx
    Avatar
    Community Member
    5 Posts

    Bumbling with LatestNews DataObject [solved] Link to this post

    Greetings,

    Just starting out SS v2.4.3, trying to implement a LatestNews function within HomePage_Controller which will allow me to pull both "news" and "announcements" (both which use ArticleHolder/ArticlePage pages). The ArticleHolders and ArticlePages all display correctly. The HomePage summary displays a single empty object each for News and Annoucements.

    I've tried more than a few dozen subtle permutations based on various forum articles and reading the DataObject documentation. Here's where I'm at:

    class HomePage_Controller extends Page_Controller {

       function LatestNews($title='news', $num=5) {
          $mynews = DataObject::get("ArticleHolder", "URLSegment = '". $title . "'");
          if (!$mynews || !$mynews->exists()) {
             echo "fail";
             return false;
          }
          return ($items) ? DataObject::get("ArticlePage", "ParentID = ". $mynews->ID, "ArticleDate DESC", "", $num) : false;
       }
    }

    In the HomePage.ss layout is the following:

    <h2>Announcements</h2>
       <ul id="NewsList">
       <% control LatestNews(announcements) %>
          <li class="newsDateTitle"><a href="$Link" title="Read more on &quot;{$Title}&quot;">$Title</a></li>
          <li class="newsDateTitle">$ArticleDate.Nice</li>
          <li class="newsSummary">$Content.FirstParagraph<a href="$Link" title="Read more on &quot;{$Title}&quot;">Read more &gt;&gt;</a></li>
       <% end_control %>
       </ul>
    <h2>Latest News</h2>
       <ul id="NewsList">
       <% control LatestNews(news) %>
          <li class="newsDateTitle"><a href="$Link" title="Read more on &quot;{$Title}&quot;">$Title</a></li>
          <li class="newsDateTitle">$ArticleDate.Nice</li>
          <li class="newsSummary">$Content.FirstParagraph<a href="$Link" title="Read more on &quot;{$Title}&quot;">Read more &gt;&gt;</a></li>
       <% end_control %>
       </ul>

    Not surprisingly, an example of the resulting HTML is:

    <h2>Latest News</h2>
       <ul id="NewsList">
          <li class="newsDateTitle"><a href="" title="Read more on &quot;&quot;"></a></li>
          <li class="newsDateTitle"></li>
          <li class="newsSummary"><a href="" title="Read more on &quot;&quot;">Read more &gt;&gt;</a></li>
       </ul>


    I've spent about 8 hours on this; I'm sure its a classic blunder, and I'll really appreciate any help!

    Bret

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Bumbling with LatestNews DataObject [solved] Link to this post

    DataObject::get("ArticleHolder", "URLSegment = '". $title . "'");

    get() returns a set. Not a single object. Therefore you cannot access the 'ID' as the ID of a set is well nothing in this case! You should use get_one() instead.

  • Parradoxx
    Avatar
    Community Member
    5 Posts

    Re: Bumbling with LatestNews DataObject [solved] Link to this post

    Willr,
    True, and thank you. But trying that yields the same result. I performed about 16 permutations with get_one() as originally spec'd in the Tutorial, then tried get() as some had complained about flaws with get_one(). For practical intent, consider this the current line of code: (which I just entered and uploaded, built and flushed, with the same result)

    $mynews = DataObject::get_one("ArticleHolder", "URLSegment = '". $title . "'");

    Everything else is the same. Next?

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Bumbling with LatestNews DataObject [solved] Link to this post

    Ok lets break down all that code to see where its failing. Using Debug::show(); you can see what is inside a variable.

    If you do Debug::show($mynews); after the get_one(); you should have a record. If you don't have a record (i.e Debug::show() is simply white) then that URLSegment = '". $title "' is not matching anything. Remove it and you should now see a ArticleHolder record in the Debug::show(); check to see what this URLSegment is.

    If you don't see any change with Debug::show() (it should be a white box at the top) ensure your site is in dev mode.

  • Parradoxx
    Avatar
    Community Member
    5 Posts

    Re: Bumbling with LatestNews DataObject [solved] Link to this post

    Willr...
    ... you da' man!

    I had to turn off xhtml strict in the Page template before the dev-mode box would show. Dev mode found an undeclared variable, that being "items". Debug::show($mynews) worked as expected (as was suspected, since I wasn't seeing the "Fail" echo, either). But after fixing the undeclared variable, everything worked fine.

    I swear, "items" is one of the last things I tried. I have no idea why it was failing in the previous 2 dozen iterations -- but your tip is what got it going. I thank you hugely! ...so do my customers!

    1220 Views
Page: 1
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.