Phalcon - Raw Query插入语法错误

时间:2016-08-03 11:34:25

标签: php mysql phalcon phalcon-orm

我有以下INSERT代码:

$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
    )
);

此代码生成以下错误消息:

  

PHP致命错误:未捕获异常'Phalcon \ Mvc \ Model \ Exception',消息'语法错误,意外令牌DESC,靠近')\ r \ n VALUES(:master_id:,:service_id:,:date:, :time:,:desc :)',解析时: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 [内部函数]:Phalcon \ Mvc \ Model \ Query-> parse() \ n#1 [内部函数]: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 [内部函数]:{closure}()\ n#4 /var/www/api/v1.0/index.php( 1961):Phalcon \ Mvc \ Micro-> handle()\ n#5 {main} \ n在1827行的/var/www/api/v1.0/index.php中抛出

什么可能导致问题?提前谢谢!

3 个答案:

答案 0 :(得分:4)

Desc是MySQL reserved word。使用Raw查询时,必须转义保留字。要在PHQL中转义它们,您必须将它们包装在方括号[]。

像这样重写您的查询将起作用:

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

请注意上面的[desc]

来自文档的信息:https://docs.phalconphp.com/en/latest/reference/phql.html#escaping-reserved-words

答案 1 :(得分:1)

请尝试使用`desc`代替desc

答案 2 :(得分:1)

我认为您遇到了问题,因为您使用的是reserved words。 最好的解决方案是为列添加一些前缀。