我对symfony2中的管理表单有疑问。所以在我的控制器中我有:
$form = $this->createForm(SubMenuType::class);
在SubMenuType类中我有:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$aTemplate = Template::$aTemplates;
$aMenu = $this->getDoctrine()->getRepository('AppAdminBundle:Menu')->findAll();
$builder
->add('name', TextType::class)
->add('template',ChoiceType::class, array('choices' => $aTemplate,'choices_as_values' => true))
->add('menu',ChoiceType::class, array('choices' => $aMenu,'choices_as_values' => true))
->add('save',SubmitType::class, array('label'=> 'Save'))
;
}
问题是:如何从$aMenu
中的Menu实体获取SubMenuType
数据?请帮我。 Thx提前
答案 0 :(得分:0)
解决问题的最简单方法是使用EntityType
表单类型:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$aTemplate = Template::$aTemplates;
$builder
->add('name', TextType::class)
->add('template',ChoiceType::class, array('choices' => $aTemplate,'choices_as_values' => true))
->add('menu', EntityType::class, array(
'class' => 'AppAdminBundle:Menu',
'choice_label' => 'property of your menu entity you want as label'
))
->add('save',SubmitType::class, array('label'=> 'Save'))
;
}