Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [SOLVED] Convert Enum to Dataobject

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

Page: 1
Go to End
Author Topic: 1023 Views
  • NickJacobs
    Avatar
    Community Member
    144 Posts

    [SOLVED] Convert Enum to Dataobject Link to this post

    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')",

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: [SOLVED] Convert Enum to Dataobject Link to this post

    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 %>

    1023 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.