任何人都可以向我展示一个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() ));}
更新:现在工作。不得不调整几点:
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() ));}解决方法 现在工作不得不调整几点:
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(),array( 'form' => $form->createVIEw() ));}总结
以上是内存溢出为你收集整理的表单 – Symfony2表单实体更新全部内容,希望文章能够帮你解决表单 – Symfony2表单实体更新所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)