以一种形式编辑多个实体 [英] Edit multiple entities in one form

查看:25
本文介绍了以一种形式编辑多个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于编辑问题 QuestionType 的实体 Question 和表单类型.我可以成功编辑一个问题.现在我创建了一个用于编辑所有问题的链接.

I have a Entity Question and form type for editing Question QuestionType. I can successfull edit one Question. Now i create a link for editing all Questions.

我想以一种形式编辑所有问题,我该如何处理?我尝试使用集合,但我不知道如何在表单集合中映射单个问题,我不确定这是否是正确的方法.

I would like to edit all Questions in one form, how can i handle this? I try to use collections, but i don't know how to map a single question in a form collection and I'm not sure if that's the right approach.

我的 QuestionType 看起来像:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $categories = $this->entityManager->getRepository('MyAppBundle:QuestionCategory')->findByIsActive(true);

        $builder->add('category', 'entity', array(
                        'class' => 'MyAppBundle:QuestionCategory',
                        'choices' => $categories,
                        'label' => 'category',
                        'translation_domain' => 'messages',
                        'multiple' => false,
                        'empty_value' => 'msg.pleaseSelect',
                        'expanded' => false))
                ->add('translations', 'a2lix_translations', array(
                            'fields' => array(
                                'text' => array(
                                    'field_type' => 'textarea',
                                    'label' => 'hintText',
                                    'attr' => array('class' => 'rte')
                                ),
                                'explanation' => array(
                                    'field_type' => 'textarea',
                                    'label' => 'title',
                                    'attr' => array('class' => '')
                                )
                            )
                ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'My\AppBundle\Entity\Question',
        ));
    }

我的控制器可以编辑所有问题,如下所示:

My Controller with action to edit all Questions, look like:

$em = $this->getDoctrine()->getManager();
$questions = $em->getRepository('MyAppBundle:Question')->findAll();

/**
  * -- Here is my problem , how can i my $questions into form? --- 
**/          
$form = $this->createFormBuilder()
    ->add('questions', 'collection', array(
            'type' => new QuestionType() ,
            'allow_add' => false,
            'allow_delete' => false,
            'label' => false)
    )
    ->add('save', 'submit', array('label' => 'Create'))
    ->getForm();


$form->handleRequest($request);

if ($form->isValid()) {
}

return $this->render('MyAppBundle:Question:editAllQuestions.html.twig', array("form" => $form->createView()));

有人有提示或方法吗?

推荐答案

您走在正确的轨道上,您可能想要做的是:

You're on the right track, what you probably want to do is:

$form->setData(array('questions' => $yourQuestionArray));

就在 ->getForm() 之后,->handleRequest($request) 之前.

Just after ->getForm() but before ->handleRequest($request).

或者你可以像这样将它作为一个选项传递给 createFormBuilder():

Or you could pass it as an option to createFormBuilder() like so:

$form = $this->createFormBuilder(array('questions' => $yourQuestionArray))
    ->add(...)

他们都做同样的事情.

通常,当您创建表单类时(看起来就像您使用 QuestionType 所做的那样),您提供一个 data_class 配置选项(参见文档) 定义了表单用作其模型的内容.

Ordinarily when you create a form class (which it looks like what you're doing with QuestionType), you provide a data_class configuration option (see documentation) that defines what the form uses as its model.

您在控制器中所做的是创建一种没有关联数据类的匿名"表单(文档,这看起来就像您在控制器中所做的一样).在这种情况下,它的数据是一个数组(类似地,如果你调用 $form->getData() 你会得到一个数组).

What you're doing in your controller is creating a kind of "anonymous" form that doesn't have an associated data class (documentation, this looks like what you're doing in your controller). In which case its data is an array (similarly if you called $form->getData() you'd get an array back).

这篇关于以一种形式编辑多个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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