从 Symfony2 和 Doctrine2 定义和使用 ENUM 类型的正确方法

2024-03-11

我在我的一张表中使用 ENUM 类型,但 Doctrine 不太喜欢它。所以我做了研究并发现了这个topic https://stackoverflow.com/questions/8750724/what-do-you-use-instead-of-enum-in-doctrine2这基本上就是在谈论它。在这个other http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/mysql-enums.htmlDoctrine 项目的文档也讨论了它和两种可能的解决方案。我将使用第一个,但是:

  1. 这段代码应该放在哪里?

    $conn = $em->getConnection();
    $conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');

  2. 稍后当我想显示包含这些值的 SELECT 时,如何从 Forms 处理这个问题?


关于此事doc http://symfony.com/doc/current/cookbook/doctrine/dbal.html#registering-custom-mapping-types您需要将这些行添加到您的配置中:

# app/config/config.yml
doctrine:
    dbal:
        connections:
            default:
                // Other connections parameters
                mapping_types:
                    enum: string

对于表单我会添加一个助手,例如getPossibleEnumValues并用它来填充构建器中的选择:

$builder->add('enumField', 'choice', array(
    'choices' => $entity->getPossibleEnumValues(),
));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 Symfony2 和 Doctrine2 定义和使用 ENUM 类型的正确方法 的相关文章

随机推荐