5

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?

Community
  • 1
  • 1
Reynier
  • 2,420
  • 11
  • 51
  • 91

3 Answers3

7

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(),
));
althaus
  • 2,009
  • 2
  • 25
  • 33
  • Thanks for your info. But how I can get access to $entity from FormBuilder? – neuromancer Apr 07 '14 at 12:37
  • There are several approaches to do this. The cleanest may be to use an `EventListener` like in this answer: http://stackoverflow.com/questions/11357748/symfony2-how-to-access-entity-values-inside-form – althaus Apr 08 '14 at 07:42
6

You shouldn't use enum (for many reasons you can find on google or here), but if you absolutely want to use enum instead of a relation with another table the best way is to emulate the enum behavior like this :

<?php
/** @Entity */
class Article
{
    const STATUS_VISIBLE = 'visible';
    const STATUS_INVISIBLE = 'invisible';

    /** @Column(type="string") */
    private $status;

    public function setStatus($status)
    {
        if (!in_array($status, array(self::STATUS_VISIBLE, self::STATUS_INVISIBLE))) {
            throw new \InvalidArgumentException("Invalid status");
        }
        $this->status = $status;
    }
}
Pierre
  • 776
  • 6
  • 27
  • hello Shady, is this right (we shouldn't use enum) also when using Symfony? – Alexis_D Jan 13 '15 at 21:19
  • 2
    @Aleqxs: In fact you never should use ENUM... Except in some particular case, for exemple if you're using an ENUM to store values for days of the week, to aid human readability, then it's a valid use. But I prefer emulate ENUM like I describe above. – Pierre Jan 16 '15 at 00:25
  • A good implementation of this style here : http://www.maxpou.fr/dealing-with-enum-symfony-doctrine/ – davidbonachera Jun 20 '17 at 04:02
1

You can create a new Doctrine Type. See the documentation about that: http://docs.doctrine-project.org/en/2.0.x/cookbook/mysql-enums.html#solution-2-defining-a-type

Once you created this type, you just have to register it by using the doctrine bundle configuration

# app/config/config.yml
doctrine:
    dbal:
        types:
            your_enum: YourApp\DBAL\YourEnum

Then you can use it on your entity like any other type :) .

Nek
  • 2,715
  • 1
  • 20
  • 34