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.

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   2605 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...