phalcon php切换更新以插入

时间:2014-10-14 00:56:14

标签: php phalcon

出于某种原因,phalcon正在将我的订单替换为插入而不是更新(我可以在日志中看到)

$phql = "UPDATE Vouchers SET user_name = :userName:, used = true WHERE voucher_code = :voucherCode:";
$status = $app->modelsManager->executeQuery($phql, array(
    'userName' => $voucherReq->userName,
    'voucherCode' => $voucherCode
));

我可能错过了一些覆盖插件更新的内容......这很多。这是一个不遵循公共ID模式的特殊模型(如果它有帮助)。

在日志中我可以看到:

[Mon Oct 13 23:49:49.698965 2014] [:error] [pid 379] [client 82.113.121.157:60949] PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value violates unique constraint "voucher_voucher_code_idx"' in /var/www/api/index.php:41\nStack trace:\n
#0 [internal function]: PDOStatement->execute()\n
#1 [internal function]: Phalcon\\Db\\Adapter\\Pdo->executePrepared(Object(PDOStatement), Array, Array)\n
#2 [internal function]: Phalcon\\Db\\Adapter\\Pdo->execute('INSERT INTO "vo...', Array, Array)\n
#3 [internal function]: Phalcon\\Db\\Adapter->insert('vouchers', Array, Array, Array)\n
#4 [internal function]: Phalcon\\Mvc\\Model->_doLowInsert(Object(Phalcon\\Mvc\\Model\\MetaData\\Memory), Object(Phalcon\\Db\\Adapter\\Pdo\\Postgresql), 'vouchers', false)\n
#5 [internal function]: Phalcon\\Mvc\\Model->save()\n
#6 [internal function]: Phalcon\\Mvc\\Model->update(Array)\n
#7 [internal function]: Phalcon\\Mvc\\Model\\Query->_executeUpdate(Array, Array, NULL)\n
#8 [internal function]: Phalcon\\Mvc\\Model\\Query->execute(Array, NULL)\n
#9 /var/www/api/index.php(41): Phalcon\\Mvc\\Model\\Manager->executeQue in /var/www/api/index.php on line 41

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我有类似的行为 似乎Phalcon需要更新where子句在主键上才能更新该特定行。我已添加id作为主键,一切正常 在您的情况下,将voucher_code设置为该表的主键 希望这有帮助