Jan Walraven
DevOps



Categories:
  • Arduino (2)
  • CSS (3)
  • Docker (11)
  • ESXi (1)
  • Git (4)
  • Google Cloud (3)
  • Javascript (6)
  • Kubernetes (4)
  • Linux (36)
  • Mac (7)
  • Magento (4)
  • Mysql (14)
  • PHP (9)
  • Zend framework 2 (9)
  • Posted on May 7, 2013

    By default enum types are not supported by doctrine.
    As explained on http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/mysql-enums.html

    The type system of Doctrine 2 consists of flyweights, which means there is only one instance of any given type. Additionally types do not contain state. Both assumptions make it rather complicated to work with the Enum Type of MySQL that is used quite a lot by developers.

    When using Enums with a non-tweaked Doctrine 2 application you will get errors from the Schema-Tool commands due to the unknown database type “enum”. By default Doctrine does not map the MySQL enum type to a Doctrine type. This is because Enums contain state (their allowed values) and Doctrine types don’t.

    This cookbook entry shows two possible solutions to work with MySQL enums. But first a word of warning. The MySQL Enum type has considerable downsides:

    • Adding new values requires to rebuild the whole table, which can take hours depending on the size.
    • Enums are ordered in the way the values are specified, not in their “natural” order.
    • Enums validation mechanism for allowed values is not necessarily good, specifying invalid values leads to an empty enum for the default MySQL error settings. You can easily replicate the “allow only some values” requirement in your Doctrine entities.

    However, you can map them to a string value and use them.

    Add the following code to configautoloaddoctrine.local.php

    
    return array(
            'doctrine' => array(
                    'connection' => array(
                            'orm_default' => array(
                                    'driverClass' =>'DoctrineDBALDriverPDOMySqlDriver',
                                    'params' => array(
                                            'host'     => '127.0.0.1',
                                            'port'     => '3306',
                                            'user'     => 'yourusername',
                                            'password' => 'yourpassword',
                                            'dbname'   => 'test',
                                    ),
                                    'doctrine_type_mappings' => array(
                                            'enum' => 'string'
                                    ),
                            )
                    )
            )
    );