【问题标题】:Symfony2 Form Entity UpdateSymfony2 表单实体更新
【发布时间】:2011-07-07 20:22:08
【问题描述】:

谁能给我看一个 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()
    ));
}

【问题讨论】:

  • 这看起来类似于我用于编辑现有实体的代码。你能更清楚你的问题吗?我不清楚你所说的“......不起作用,因为它覆盖了实体......”
  • 函数中获取推荐的第二行尝试根据传入的 $id 进行查找。当 POST 发生时它没有找到,因此为 $testimonial 返回 null。我只是尝试更改我的代码以在帖子发生时在名为“id”的表单中查找隐藏字段。这似乎让我更进一步,但后来它抱怨我的实体中的 id 是私有的,并建议我在我的实体中创建一个 setId() 方法。显然控制台出于某种原因没有为我创建一个。
  • 搞定了。请参阅上面帖子中的更新。
  • 杰里米,您介意将您的“更新”部分放在一个新的答案中并接受这个。那么这个问题不会被列在未回答的下面:-)

标签: forms entity symfony


【解决方案1】:

现在工作。不得不调整一些东西:

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()
    ));
}

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 这是真的,但它确实将它分成两个控制器动作。
    • 我知道这是一篇旧帖子,但我不得不感谢你!我仍然像个傻瓜一样坐在这里写我的 CRUD 代码!谢谢好心的先生!
    【解决方案3】:

    通过 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-07
      • 2015-08-15
      相关资源
      最近更新 更多