没有为触发错误引发PDO异常 [英] PDO Exception not being thrown for trigger errors

查看:55
本文介绍了没有为触发错误引发PDO异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将PDO与OCI驱动程序配合使用.我的特定 INSERT 查询导致触发错误:

第1行的

  ERROR:ORA-01403:找不到数据ORA-06512:位于"FOO.BAR"的第5行ORA-04088:执行触发器'FOO.BAR'时出错 

但是,发生这种情况时不会引发异常.我正在使用 ERRMODE_EXCEPTION ,对于标准查询语法错误等,抛出异常很好,但是对于源自触发器的任何错误,不是.

在这种情况下,对 $ st-> execute()的调用也不返回 FALSE .此外, $ st-> errorInfo() $ st-> errorCode()也是无济于事的:

  array(3){[0] =>字符串(5)"00000"[1] =>空值[2] =>空值}字符串(5)"00000" 

当触发错误发生时,如何让PDO引发异常?或者,如果那是不可能的,我该如何以编程方式检测何时通过其他方式发生这种情况?我什至知道,首先发生错误(除了未更新数据库之外)的唯一方法是 $ st-&row; rowCount()报告为0.但这不是唯一的或通过可靠的方式自行确定是否发生错误...

解决方案

这是不可能的.触发器(和事件)基于数据库操作在数据库上运行,并且不直接与您的查询相关联-至少从PHP方面可以看出.您的PDO查询成功了,这就是您的语句所知道的一切,事实上,将触发器设置为根据您的语句对某个表执行的操作的结果来做某事是无关紧要的.

您拥有的选项是:

  1. 编写另一个查询以确认触发器应该执行的操作是否成功
  2. 可能将初始查询包装到存储过程中,该存储过程将运行查询并验证触发操作是否成功.然后调用存储的proc,而不是直接运行查询.

I am using PDO with the OCI driver. A particular INSERT query of mine is causing a trigger error:

ERROR at line 1:
ORA-01403: no data found
ORA-06512: at "FOO.BAR", line 5
ORA-04088: error during execution of trigger 'FOO.BAR'

However, an exception is not being thrown when this happens. I am using ERRMODE_EXCEPTION, and exceptions are thrown fine for standard query syntax errors and such, but not for any error originating from a trigger.

The call to $st->execute() does not return FALSE in this case either. Also, $st->errorInfo() and $st->errorCode() are both unhelpful, as well:

array(3) {
  [0]=>
  string(5) "00000"
  [1]=>
  NULL
  [2]=>
  NULL
}
string(5) "00000"

How can I get PDO to throw an exception when a trigger error happens? Or, if that's impossible, how can I programmatically detect when this happens by other means? The only way I even know that an error happened in the first place (aside from the database not being updated) is that $st->rowCount() is reporting 0. But this is not a unique or reliable way by itself of determining whether an error occurred...

解决方案

It's impossible. Triggers (and events) are run on the DB based on DB actions and are not directly tied your query--at least as seen on the PHP side. Your PDO query is succeeding and that's all your statement will ever know, the fact that a trigger was set up to do something based on the results of whatever your statement did to some table is irrelevant.

The options you have are:

  1. write another query to confirm the success of whatever the trigger was supposed to do
  2. possibly wrap the initial query into a stored procedure that runs the query and verifys the success of the trigger action. Then call that stored proc instead of running the query directly.

这篇关于没有为触发错误引发PDO异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆