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

[SOLVED] Convert Enum to Dataobject


Go to End


2 Posts   2105 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 %>