定义和使用Symfony2和Doctrine2中的ENUM类型的正确方法 [英] Right way to define and work with ENUM types from Symfony2 and Doctrine2

查看:85
本文介绍了定义和使用Symfony2和Doctrine2中的ENUM类型的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一张表中使用了ENUM类型,但是Doctrine不太喜欢它.因此,我进行了研究,发现了这个主题,其中基本上谈论它.在此其他文档中来自Doctrine项目的项目还讨论了它以及两种可能的解决方案.我将使用第一个,但:

I'm using ENUM type in one of my tables but Doctrine doesn't like it so much. So I do my research and found this topic which basically talks about it. In this other doc from Doctrine project also talks about it and two possibles solutions. I'll use the first one but:

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

  1. Where it's supposed this code should go?

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

以后要显示具有这些值的SELECT时如何处理Forms中的内容?

How do I deal with this from Forms later when I want to display a SELECT with those values?

推荐答案

关于此文档,则需要将以下几行添加到您的配置中:

Regarding this doc you need to add these lines to your config:

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

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

For the forms I'd add a helper like getPossibleEnumValues and use this to fill the choices in the builder:

$builder->add('enumField', 'choice', array(
    'choices' => $entity->getPossibleEnumValues(),
));

这篇关于定义和使用Symfony2和Doctrine2中的ENUM类型的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆