我有一个Doctrine extensions tree Entity,我希望将它完全放在一个表单中(或者只是一个节点及其所有子节点)。也就是说,我希望能够以单一形式修改整个(子)树。我看了一下“multiple rows in form for the same entity in symfony2”,但是,我无法将它应用到Symfony3中所有孩子的树上。
我在考虑像
这样的控制器$repository = $this->getDoctrine()->getRepository('AppBundle:Category');
$tree = $repository->children(null, true);
$form = $this->createForm(CategoryType::class, $tree);
和CategoryType
之类的
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Category::class /* or should it be `null`? */,
));
}
答案 0 :(得分:1)
使用以下控制器:
public function editAction(Request $request)
{
$repository = $this->getDoctrine()->getRepository('AppBundle:Category');
$categories = $repository->children(null, false); // get the entire tree including all descendants
$form = $this->createFormBuilder(array('categories' => $categories));
$form->add('categories', CollectionType::class, array(
'entry_type' => CategoryType::class,
));
$form->add('edit', SubmitType::class);
$form = $form->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
// $data['categories'] contains an array of AppBundle\Entity\Category
// use it to persist the categories in a foreach loop
}
return $this->render(...)
}
CategoryType
就像'正常',例如,我问题中的那个。
使用array('categories' => $categories)
创建表单构建器并添加名为CollectionType
的表单categories
字段是关键。