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.

General Questions /

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

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

silverstripe 3 dataobject foreach add property


Reply


3 Posts   817 Views

Avatar
Futureweb

Community Member, 233 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, 5513 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
Futureweb

Community Member, 233 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...