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

[SOLVED] Convert Enum to Dataobject


Go to End


2 Posts   2767 Views

Avatar
NickJacobs

Community Member, 148 Posts

28 January 2011 at 11:33am

Hi... I need to convert db enum values to a dataobject so I can loop through them in a template...can anyone help with this??
Enum looks like:

 'Make' => "Enum('Ford,Great Wall,Holden,Isuzu,Mazda,Mitsubishi,Nissan,Toyota,VW')",

Avatar
NickJacobs

Community Member, 148 Posts

28 January 2011 at 12:33pm

ok....found it with a bit more searching and mucking around :)

public function BuildVehicleMenu() { 
$doSet = new DataObjectSet(); 
foreach(singleton('VehicleItem')->dbObject('Make')->enumValues() as $make) { 
$doSet->push(new ArrayData(array( 
'MakeName' => $make, 
'Vehicles' => DataObject::get("VehicleItem","Make = '$make'") 
))); 
}

return $doSet; 
}

template example:

<% control BuildVehicleMenu %> 
$MakeName 
<% control Vehicles %> 
<a href="$link">$Model</a>
<% end_control %> 
<% end_control %>