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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

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

SS3 Adding data to DataObject in page controller

Go to End

2 Posts   922 Views


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;


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.