40

When running doctrine:mapping:import i get an error:

Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it.

It seems I need to set use_native_enum to true some how. However, all documentation and blog posts are refering to Symfony < 1.4. Is there any what would be the solution in Symfony 2?

j0k
  • 22,600
  • 28
  • 79
  • 90
Roel Veldhuizen
  • 4,613
  • 8
  • 44
  • 78

2 Answers2

124

For Symfony 2 projects, add this to the doctrine dbal configuration in app/config.yml:

doctrine:
    dbal:
        mapping_types: 
            enum:       string 

My full doctrine config looks like this:

# Doctrine Configuration
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%
        charset:  UTF8
        mapping_types:
            enum: string
            set: string
            varbinary: string
            tinyblob: text

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true

Code adapted from here

Then run:

app/console doctrine:schema:update --force --dump-sql --ansi

crmpicco
  • 16,605
  • 26
  • 134
  • 210
Plínio César
  • 5,965
  • 3
  • 23
  • 30
  • 7
    yes this works but issue i guess is it erases all the enum types from the database when you do a "php app/console doctrine:schema:update --force" – mahen3d May 08 '14 at 06:26
  • 1
    Came across this issue with a Symfony 4 app using `doctrine:migrations:diff` and this answer still sorts it :-) – Bananaapple Dec 07 '18 at 10:12
  • The strange thing that happened to me was that, the migration containing the `enum` was migrated successfully but i couldn't migrate any migration after that! – Sodj Mar 10 '19 at 14:53
  • I am going to run this command `php bin/console make:migration` but it displaying error like: Unknown database type enum requested, Doctrine\DBAL\Platforms\MySQL57Platform may not support it. – Raghav Rangani Jul 02 '19 at 10:33
  • Worked fine for me, Thanks mate – John Apr 25 '20 at 21:07
-1

Considering the Doctrine cookbook only provides partial answers as to how to make enums interpret as strings, the following should work regardless of how Doctrine is configured.

The error points you at the name of the file: Doctrine\DBAL\Platforms\MySqlPlatform.php - in there, you'll find that the default list is embedded in the function initializeDoctrineTypeMappings as follows:

$this->doctrineTypeMapping = array(
            'tinyint'       => 'boolean',
            'smallint'      => 'smallint',
            'mediumint'     => 'integer',
            'int'           => 'integer',
            (...)

Adding simple enum support for all doctrine users, regardless of the rest of the setup, is simply achieved by extending the list with:

'enum' => 'string'
Combuster
  • 583
  • 5
  • 19
  • 3
    It is a bad idea to change the code from "third" parties. When you want to update this code (which you want: new features, security etc.) your changes will also be replaced. – remi Feb 12 '19 at 20:12