Symfony2 - Doctrine - 在实体管理器刷新调用时捕获错误

时间:2016-12-15 14:25:41

标签: symfony exception doctrine

我正在尝试更新AJAX

中同一个表的两个记录/行
public function reorderApplications(Request $request)
{
    if (!$request->isXmlHttpRequest()) {
        return new JsonResponse(array('message' => 'You can access this only using Ajax!'), 400);
    }

    $from = $request->request->get('from');
    $to = $request->request->get('to');

    $em = $this->getDoctrine()->getManager();
    /** @var Application $fromApplication */
    $fromApplication = $em->getRepository('IndexBundle:Application')->find($from['application']);
    /** @var Application $toApplication */
    $toApplication = $em->getRepository('IndexBundle:Application')->find($to['application']);

    try {
        $fromApplication->setOrder($to['position']);
        $toApplication->setOrder($from['position']);

        $em->flush();
        $response = array(
            'response' => 'success',
            'message' => 'Applications were reordered successfully.'
        );
    } catch (\Exception $e) {
        $response = array(
            'response' => 'error',
            'message' => $e->getMessage()
        );
    }

    return new JsonResponse($response, 200);
}

最终,try$em->flush()失败,我收到错误:

  

执行'UPDATE application SET order =时发生异常   ? WHERE id =?' with params [“3”,4]:↵↵SQLSTATE[42000]:语法错误   或访问冲突:1064您的SQL语法有错误;校验   与您的MySQL服务器版本对应的手册   在'order ='3'WHERE id = 4'第1行附近使用的语法

任何人都知道可能出现什么问题?

2 个答案:

答案 0 :(得分:2)

order是mysql中的保留关键字。尝试重命名该字段。

http://dev.mysql.com/doc/refman/5.7/en/keywords.html

答案 1 :(得分:2)

order是SQL中的保留关键字。

要在列名中使用它,必须将反引号放在定义中:

@Column(name="`order`")