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.

    评论

报告相同问题?

悬赏问题

  • ¥15 BV260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序