Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » SS3 Adding data to DataObject in page controller

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

Page: 1
Go to End
Author Topic: 460 Views
  • nekranox
    Avatar
    Community Member
    31 Posts

    SS3 Adding data to DataObject in page controller Link to this post

    Hey guys,

    Before SS3, I could load in a dataobject and loop through each item adding fields on the fly which could then be used in the template.

    I can't seem to get this working in SS3, any ideas why?

       public function Shops(){
          $Shops = Shop::get();
          foreach($Shops as &$Shop) {
             $Shop->URL = trim(preg_replace('/[^a-z0-9]+/', '-', strtolower($Shop->Title)), '-');
          }
          return $Shops;
       }

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: SS3 Adding data to DataObject in page controller Link to this post

    Why would you iterate over the set only to iterate over it again most likely in your template?

    A better way to structure it would be to add a method to your Shop object which encapsulates this functionality.

    public function getURL() {
    return trim(preg_replace('/[^a-z0-9]+/', '-', strtolower($Shop->Title)), '-');
    }

    Then you can use $URL where you require, not just that template.

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