phpmysqlphalconphalcon-orm

Phalcon - Raw Query Insert Syntax error


I've got the following INSERT code :

$reservationSuccess = $app->modelsManager->executeQuery( 
    "INSERT INTO MastersReservedTime (master_id, service_id, date, time, desc)
     VALUES (:master_id:, :service_id:, :date:, :time:, :desc:)",
    array(
        'master_id' => $OrderCommit->master_id,
        'service_id' => $oneService -> service_id,
        'date' => $OrderCommit->date,
        'time' => $startTime,
        'desc' => $desc
    )
);

And this code generates the following error message :

PHP Fatal error: Uncaught exception 'Phalcon\Mvc\Model\Exception' with message 'Syntax error, unexpected token DESC, near to ')\r\n VALUES (:master_id:, :service_id:, :date:, :time:, :desc:)', when parsing: INSERT INTO MastersReservedTime (master_id, service_id, date, time, desc)\r\n VALUES (:master_id:, :service_id:, :date:, :time:, :desc:) (209)' in /var/www/api/v1.0/index.php:1827\nStack trace:\n#0 [internal function]: Phalcon\Mvc\Model\Query->parse()\n#1 [internal function]: Phalcon\Mvc\Model\Query->execute()\n#2 /var/www/api/v1.0/index.php(1827): Phalcon\Mvc\Model\Manager->executeQuery('INSERT INTO Mas...', Array)\n#3 [internal function]: {closure}()\n#4 /var/www/api/v1.0/index.php(1961): Phalcon\Mvc\Micro->handle()\n#5 {main}\n thrown in /var/www/api/v1.0/index.php on line 1827

What could cause the problem? Thanks in advance!


Solution

  • Desc is MySQL reserved word. You have to escape reserved words when you are using Raw queries. To escape them in PHQL you have to wrap them in square brackets [].

    Rewriting your query like this will work:

    INSERT INTO MastersReservedTime (master_id, service_id, date, time, [desc])
    VALUES (:master_id:, :service_id:, :date:, :time:, :desc:)
    

    Note the [desc] above.

    Info from the docs: https://docs.phalconphp.com/en/latest/reference/phql.html#escaping-reserved-words