How to enable ENUMs in Symfony 2 / Doctrine

前端 未结 2 1380
一整个雨季
一整个雨季 2020-12-02 11:17

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

Unknown database type enum requested, Doctrine\\DBAL\\Platforms\\MySqlPlatform may n

相关标签:
2条回答
  • 2020-12-02 11:50

    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'
    
    0 讨论(0)
  • 2020-12-02 11:59

    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

    0 讨论(0)
提交回复
热议问题