Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2,Doctrine2,更新实体_Php_Jquery_Ajax_Symfony - Fatal编程技术网

Php Symfony2,Doctrine2,更新实体

Php Symfony2,Doctrine2,更新实体,php,jquery,ajax,symfony,Php,Jquery,Ajax,Symfony,我想通过对控制器的ajax调用将一个实体更新到db中。然而,我只知道如何使用Symfony的表单更新实体。目前,我有一个表单,它将被jQuery方法附加,并通过ajax提交,但我不知道在控制器中该做什么 阿贾克斯: 现在在我的控制器上: /** * @Route("/edit/", name="containers_edit", defaults={"_format" = "json"}) */ public function editCtr(Request $request) {

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

阿贾克斯:

现在在我的控制器上:

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

对此有什么建议吗?

检查评论中的链接,它提供了您需要的一切

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

editCtr()
editCtrAction()


控制器类中的所有方法都是“actions”。希望这能有所帮助。

就像有普通表单一样,只是别忘了在jQuery中序列化数据。看看这里——到底是什么不起作用?
/**
 * @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));
}