我在这里不知所措。我需要知道如何在违反完整性约束的情况下处理错误消息。
意思是我想向用户显示一些有意义的消息,而不是显示错误消息,例如
Error: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails
我需要捕获这些数据库错误并只显示类似这样的消息
The item you are trying to delete is associated with other records
我们该如何处理这个问题。
我在这里找到了参考:https://stackoverflow.com/a/8842963/576523
但我不想进行计数检查。
当我们使用 debug_kit 插件时,我们可以看到他们已经捕获了这些值
变量选项卡。我需要一种方法来执行此操作或从 debug_kit 插件访问这些数据。
Thankz.
你也可以使用try
- catch
try {
$this->Item->delete();
} catch (Exception $e) {
$error = 'The item you are trying to delete is associated with other records';
// The exact error message is $e->getMessage();
$this->set('error', $error);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)