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.

Data Model Questions /

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

SS3 Adding data to DataObject in page controller


Go to End


2 Posts   1329 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, 5523 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.