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.

Data Model Questions /

SS3 Adding data to DataObject in page controller


Reply


2 Posts   561 Views

Avatar
nekranox

Community Member, 31 Posts

23 July 2012 at 8:38pm

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

Avatar
Willr

Forum Moderator, 5513 Posts

23 July 2012 at 10:57pm

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.