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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

silverstripe 3 dataobject foreach add property


Go to End


3 Posts   2112 Views

Avatar
JonShutt

Community Member, 244 Posts

2 April 2013 at 12:37pm

in my controllor I have:

$pages = DataObject::get("Page", "`ParentID` = '$ID'");
		
	if ($pages) {
		foreach ($pages as $page) {						
			$page->extraInfo= 'blahblah';
			}
		}
	}
	return  $pages;

this worked fine in SS2.4, but when I loop the dataobject in a template in ss3, there is no value for $extraInfo... I know the ORM works a bit differently now, do I have to do something else to 'add' values to the dataobject returned to the template?

Cheers

Avatar
Willr

Forum Moderator, 5523 Posts

3 April 2013 at 9:17pm

The new ORM handles DataObject::get() in a much smarter way in regards to memory. To go down the path you're wanting to do, you can either move extraInfo to a getExtraInfo() method on Page to keep it encapsulated, or you'll have to create a new ArrayList object and push each of the Dataobjects to the new arraylist with the new property

$output = new ArrayList();

foreach($pages as $page) {
$page->ExtraInfo = "";
$output->push($page);
}
..

Avatar
JonShutt

Community Member, 244 Posts

4 April 2013 at 9:41am

Hi Will,

That makes sense, cheers for the new ArrayList idea.

In the end I've gone for creating an function which is called from within the <% loop %> in the template.
However, I had to put this getExtraInfo() function in the model, not the controller for it to be called by the template...