在Symfony 2.7中浏览Choice表单字段界面的文档时,我会在top of this page注意以下代码段:
$builder->add('attending', 'choice', array(
'choices' => array(
Status::getInstance(Status::YES),
Status::getInstance(Status::NO),
Status::getInstance(Status::MAYBE),
),
'choices_as_values' => true,
'choice_label' => 'displayName',
));
Status
本质上是在PHP中实现一个枚举类 . 在Symfony中似乎没有相关的接口 . 有谁知道如何优雅地实现像 Status
这样的东西,并允许在一个地方添加更多的值?
2 回答
如果需要,可以使用Status作为抽象类,从该类继承所有Status并返回相应的对象
通过交响乐,我们可以使用关于您的实体的注释和学说关怀 . 别忘了将'Doctrine \ ORM \ Mapping称为ORM'
我之前没有看到过这个帖子有同样的问题,所以如果我能帮助别人的话!