Jump to:

23368 Posts in 18143 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » silverstripe 3 dataobject foreach add property

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

Page: 1
Go to End
Author Topic: 670 Views
  • Futureweb
    Avatar
    Community Member
    232 Posts

    silverstripe 3 dataobject foreach add property Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: silverstripe 3 dataobject foreach add property Link to this post

    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);
    }
    ..

  • Futureweb
    Avatar
    Community Member
    232 Posts

    Re: silverstripe 3 dataobject foreach add property Link to this post

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

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