dtcmadj31951 2014-09-05 17:43
浏览 44

Symfony2,Doctrine2,更新实体

I want to update an entity into the db with an ajax call to the controller. However, I only know how to update entity using Symfony's form. Currently I have a form that will be appended by a jQuery method and submit it through ajax but am lost at what to do in the controller.

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

Now on my controller:

/**
 * @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));
}

Any suggestions for this?

  • 写回答

1条回答 默认 最新

  • dongwang6837 2014-09-05 18:20
    关注

    Check the link in the comment by dmnptr it has everything you need.

    Just one thing to add, in your code you'll need to change the method name from

    editCtr() to editCtrAction()

    all methods in the controller class are 'actions'. Hope this helps.

    评论

报告相同问题?