Symfony2表单类型实体:未选择选项

时间:2013-11-26 21:44:21

标签: symfony doctrine-orm twig

我使用Symfony2(2.3)实体表单类型从Doctrine实体加载HTML select的选项:

Class RoomFacilityType:

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add('title', 'text', array('label' => 'Bezeichnung', 'required' => false))
            ->add('save', 'submit', array('label' => 'Speichern'));

    // add referenced object facility type
    $builder->add('facilityType', new FacilityTypeType());
}

Class FacilityTypeType:

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add('type', 'entity', array('label' => 'Ausstattung', 'class' => 'xyz\abcBundle\Entity\FacilityType', 'property' => 'type'));
}

在控制器中,我创建了一个包含RoomFacility实体的表单:

$formRoomFacility = $this->createForm(new RoomFacilityType(), $roomFacility);

RoomFacility实体与实体FacilityType具有ManToOne关系。

当表单由树枝模板呈现时,没有选择HTML选项的选项(但是当我使用文本表单类型时,显示正确的值):

{{ form_widget(formRoomFacility.facilityType.type) }}

我发现了有关如何通过明确设置选项来设置选项的问题,但这也无法解决我的问题:

$formRoomFacility->get('facilityType')->setData($roomFacility->getFacilityType());

有什么建议吗?

提前致谢

亚历

1 个答案:

答案 0 :(得分:0)

以下可能会做你想做的事:

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add('title', 'text', array('label' => 'Bezeichnung', 'required' => false))
            ->add('type', 'entity', array('label' => 'Ausstattung', 'class' => 'xyz\abcBundle\Entity\FacilityType', 'property' => 'type'))
            ->add('save', 'submit', array('label' => 'Speichern'))
    ;
}