Symfony2,Doctrine2,更新实体


Symfony2, Doctrine2, updating entity

我想通过对控制器的ajax调用将实体更新到数据库中。然而,我只知道如何使用Symfony的表单更新实体。目前我有一个表单,它将通过jQuery方法附加并通过ajax提交,但我不知道在控制器中该做什么。

Ajax:

$("#editctrno").on("submit", function(event) {
     event.preventDefault();
     $.ajax({
         url: "{{ path('containers_edit') }}",
         type: "POST",
         data: {'ctrno' : $("#ctrno").val(),
                'refno' : $("#refno").val()},
         dataType: "json",
         success: function(data) {
             console.log(data[0].ctrno);
         }
   });
});

现在在我的控制器上:

/**
 * @Route("/edit/", name="containers_edit", defaults={"_format" = "json"})
 */
public function editCtr(Request $request) {
    $ctrno = $request->get('ctrno');
    $refno =  $request->get('refno');
    $em = $this->getDoctrine()->getManager()->getRepository('Bundle:Ref');
    // I want to access in the database to find the 'refno' and update the 'ctrno', 
    // something like:
    // $entity = $em->findRefno($refno);
    // $entity->setCtrno($ctrno);
    // $em->flush();
    return new Response(json_encode($entity));
}

对此有什么建议吗?

检查dmnptr注释中的链接,它已经提供了所需的一切。

只需要添加一件事,在您的代码中,您需要更改中的方法名称

editCtr()editCtrAction()

控制器类中的所有方法都是"操作"。希望这能有所帮助。