The controller should return a response in Symfony2

I have ajax code in my code. What I want to achieve with a challenge works fine. I want to delete some records from the database, which is actually deleted when the method is called via ajax, but, like in the symfony method, it should return a response, and that is why when the method is executed, it gives me an error

My Ajax call

$.ajax({ type: "POST", data: data, url:"{{ path('v2_pm_patents_trashpatents') }}", cache: false, success: function(){ document.location.reload(true); } }); 

And the executable method

  public function trashpatentAction(Request $request){ if ($request->isXmlHttpRequest()) { $id = $request->get("pid"); $em = $this->getDoctrine()->getEntityManager(); $patent_group = $em->getRepository('MunichInnovationGroupPatentBundle:PmPatentgroups')->find($id); if($patent_group){ $patentgroup_id = $patent_group->getId(); $em = $this->getDoctrine()->getEntityManager(); $patents = $em->getRepository('MunichInnovationGroupPatentBundle:SvPatents') ->findBy(array('patentgroup' => $patentgroup_id)); if($patents){ foreach($patents as $patent){ if($patent->getIs_deleted()==false){ $patent->setIs_deleted(true); $em->flush(); } } } $patent_group->setIs_deleted(true); $em->flush(); } else{ $em = $this->getDoctrine()->getEntityManager(); $patent = $em->getRepository('MunichInnovationGroupPatentBundle:SvPatents')->find($id); if ($patent) { $patent->setIs_deleted(1); $em->flush(); } } return true; } } 

How can I successfully return from this method? Any ideas? Thanks

+7
source share
1 answer

Replace return true; on return new Response(); . Also be sure to write use Symfony\Component\HttpFoundation\Response; up.

+45
source

All Articles