php 如何在 Symfony 2 / Doctrine 中启用 ENUM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8312271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable ENUMs in Symfony 2 / Doctrine
提问by Roel Veldhuizen
When running doctrine:mapping:import
i get an error:
运行时doctrine:mapping:import
出现错误:
Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it.
请求的数据库类型未知,Doctrine\DBAL\Platforms\MySqlPlatform 可能不支持。
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?
看来我需要设置use_native_enum
到true
一些如何。但是,所有文档和博客文章都参考 Symfony < 1.4。Symfony 2 中有什么解决方案吗?
回答by PutzKipa
For Symfony 2 projects, add this to the doctrine dbal configuration in app/config.yml
:
对于 Symfony 2 项目,将其添加到 dbal 配置中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
代码改编自here
Then run:
然后运行:
app/console doctrine:schema:update --force --dump-sql --ansi
app/console doctrine:schema:update --force --dump-sql --ansi
回答by Combuster
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.
考虑到 Doctrine 说明书仅提供了关于如何将枚举解释为字符串的部分答案,无论 Doctrine 是如何配置的,以下内容都应该起作用。
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:
错误指向文件名:Doctrine\DBAL\Platforms\MySqlPlatform
.php - 在那里,您会发现默认列表嵌入在函数中initializeDoctrineTypeMappings
,如下所示:
$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'