Ignas Damunskis
Ignas Damunskis

Reputation: 1514

Symfony2 - Doctrine - error catched on entity manager flush call

I am trying to update two records/rows of same table in 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);
}

Eventually, try fails at $em->flush() and I get error:

An exception occurred while executing 'UPDATE application SET order = ? WHERE id = ?' with params ["3", 4]:↵↵SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order = '3' WHERE id = 4' at line 1

Anyone knows what could be the problem?

Upvotes: 1

Views: 1339

Answers (2)

Bartosz Zasada
Bartosz Zasada

Reputation: 3900

order is a reserved keyword in SQL.

To use it in column name, you must put backquotes in the definition:

@Column(name="`order`")

Upvotes: 3

janrop
janrop

Reputation: 301

order is a reserved keyword in mysql. Try renaming the field.

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

Upvotes: 2

Related Questions