Right way to define and work with ENUM types from Symfony2 and Doctrine2

Reynier picture Reynier · Aug 5, 2013 · Viewed 17.4k times · Source

I'm using ENUM type in one of my tables but Doctrine doesn't like it so much. So I do my research and found this topic which basically talks about it. In this other doc from Doctrine project also talks about it and two possibles solutions. I'll use the first one but:

  1. Where it's supposed this code should go?

    $conn = $em->getConnection();
    $conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');

  2. How do I deal with this from Forms later when I want to display a SELECT with those values?

Answer

althaus picture althaus · Aug 6, 2013

Regarding this doc you need to add these lines to your config:

# app/config/config.yml
doctrine:
    dbal:
        connections:
            default:
                // Other connections parameters
                mapping_types:
                    enum: string

For the forms I'd add a helper like getPossibleEnumValues and use this to fill the choices in the builder:

$builder->add('enumField', 'choice', array(
    'choices' => $entity->getPossibleEnumValues(),
));