cakephp - 如何处理违反完整性约束的错误 [英] cakephp - How to handle integrity constraint violation errors

查看:42
本文介绍了cakephp - 如何处理违反完整性约束的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Am at a loss here. I need to know how to handle error messages in case of integrity constraint violations.

意思是我想向用户展示一些有意义的消息,而不是显示像

Meaning i want to show users some meaningful message instead displaying error messages like

Error: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails

我需要捕获这些数据库错误并只显示诸如 say 之类的消息

I need to capture these databse errors and just show messages like say

The item you are trying to delete is associated with other records 

我们该如何处理.

我在这里找到了一个参考:https://stackoverflow.com/a/8842963/576523

i have found a refernce here : https://stackoverflow.com/a/8842963/576523

但我不想做计数检查.

当我们使用 debug_kit 插件时,我们可以看到他们在

When we use the debug_kit plugin we can see that they have captured these values under the

变量选项卡.我需要一种方法来执行此操作或从 debug_kit 插件访问这些数据.

variables tab. I need a way to do this or access these data from the debug_kit plugin.

谢谢.

推荐答案

你也可以使用 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 - 如何处理违反完整性约束的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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