cakephp - 如何处理违反完整性约束的错误

标签 cakephp cakephp-2.0 cakephp-2.3

在这里不知所措。我需要知道如何在违反完整性约束的情况下处理错误消息。

这意味着我想向用户显示一些有意义的消息,而不是显示错误消息,例如

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/

相关文章:

mysql - 从 cake 2.6 切换到 2.9 时编码错误

php - 更新 CakePHP - 在此服务器上找不到请求的 URL

google-app-engine - _ah/URL 通过 app.yaml 传递到应用程序

cakePHP 中未应用 Javascript

mysql - 在 CakePHP 中查询大型 i18n 表很慢

mysql - cakephp 连接远程 MySql

unit-testing - CakePHP单元测试无法识别固定装置

cakephp - 本地化不起作用

php - CakeEmail - 我该如何使用它?

php - 使用 cakephp save 在 mysql 数据库中保存十进制值时出现问题