Symfony2表单实体更新

时间:2011-07-07 20:22:09

标签: forms entity symfony

有人可以告诉我一个Symfony2表单实体更新的具体示例吗?本书仅介绍如何创建新实体。我需要一个如何更新现有实体的示例,其中我最初在查询字符串上传递实体的id。

我无法理解如何在检查帖子的代码中再次访问表单而无需重新创建表单。

如果我重新创建表单,这意味着我还必须再次查询该实体,这似乎没有多大意义。

这是我目前所拥有的,但它不起作用,因为它在表单发布时覆盖实体。

public function updateAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();
    $testimonial = $em->getRepository('MyBundle:Testimonial')->find($id);
    $form = $this->createForm(new TestimonialType(), $testimonial);

    $request = $this->get('request');
    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        echo $testimonial->getName();

        if ($form->isValid()) {
            // perform some action, such as save the object to the database
            //$testimonial = $form->getData();
            echo 'testimonial: ';
            echo var_dump($testimonial);
            $em->persist($testimonial);
            $em->flush();

            return $this->redirect($this->generateUrl('MyBundle_list_testimonials'));
        }
    }

    return $this->render('MyBundle:Testimonial:update.html.twig', array(
        'form' => $form->createView()
    ));
}

3 个答案:

答案 0 :(得分:16)

现在工作。不得不调整一些事情:

public function updateAction($id)
{
    $request = $this->get('request');

    if (is_null($id)) {
        $postData = $request->get('testimonial');
        $id = $postData['id'];
    }

    $em = $this->getDoctrine()->getEntityManager();
    $testimonial = $em->getRepository('MyBundle:Testimonial')->find($id);
    $form = $this->createForm(new TestimonialType(), $testimonial);

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            // perform some action, such as save the object to the database
            $em->flush();

            return $this->redirect($this->generateUrl('MyBundle_list_testimonials'));
        }
    }

    return $this->render('MyBundle:Testimonial:update.html.twig', array(
        'form' => $form->createView()
    ));
}

答案 1 :(得分:10)

这实际上是Symfony 2的原生功能:

您可以从命令行自动生成CRUD控制器(通过doctrine:generate:crud)并重用生成的代码。

此处的文档: http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_doctrine_crud.html

答案 2 :(得分:1)

通过Symfony的命令generate:doctrine:crud快速查看自动生成的CRUD代码,显示以下编辑操作的源代码

/**
     * Displays a form to edit an existing product entity.
     *
     * @Route("/{id}/edit", name="product_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, Product $product)
    {
        $editForm = $this->createForm('AppBundle\Form\ProductType', $product);
        $editForm->handleRequest($request);
        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->getDoctrine()->getManager()->flush();
            return $this->redirectToRoute('product_edit', array('id' => $product->getId()));
        }
        return $this->render('product/edit.html.twig', array(
            'product' => $product,
            'edit_form' => $editForm->createView(),
        ));
    }

请注意,Doctrine实体将传递给操作而不是id(字符串或整数)。这将进行隐式参数转换,并使您无法手动获取具有给定id的相应实体。

在Symfony的文档中提到best practice