在这里不知所措。我需要知道如何在违反完整性约束的情况下处理错误消息。
这意味着我想向用户显示一些有意义的消息,而不是显示错误消息,例如
Error: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails
我需要捕获这些数据库错误并只显示诸如 say 之类的消息
The item you are trying to delete is associated with other records
我们如何处理这个问题。
我在这里找到了一个引用:https://stackoverflow.com/a/8842963/576523
但我不想做计数检查。
当我们使用 debug_kit 插件时,我们可以看到他们已经在
变量选项卡。我需要一种方法来执行此操作或从 debug_kit 插件访问这些数据。
谢谢。
最佳答案
您也可以使用 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);
}
关于cakephp - 如何处理违反完整性约束的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21300127/