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.

Data Model Questions /

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

Bumbling with LatestNews DataObject [solved]


Go to End


5 Posts   1992 Views

Avatar
Parradoxx

Community Member, 5 Posts

7 January 2011 at 6:11pm

Edited: 07/01/2011 6:17pm

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

Avatar
Willr

Forum Moderator, 5523 Posts

7 January 2011 at 7:07pm

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.

Avatar
Parradoxx

Community Member, 5 Posts

7 January 2011 at 7:41pm

Edited: 07/01/2011 7:44pm

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?

Avatar
Willr

Forum Moderator, 5523 Posts

8 January 2011 at 12:45pm

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.

Avatar
Parradoxx

Community Member, 5 Posts

8 January 2011 at 3:19pm

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!