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

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

问题描述

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



这意味着我想向用户显示一些有意义的消息,而不是显示类似

 错误:SQLSTATE [23000]:完整性约束违例:1451无法删除或更新父行:外键约束失败



我需要捕获这些数据库错误,并显示像

这样的消息

 您尝试删除的项目与其他记录相关联

我们如何处理这个问题。



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



但我不想进行计数检查。



当我们使用debug_kit插件时,我们可以看到他们已经捕获了



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



谢谢。

解决方案

您也可以使用 try - catch

  try {
$ this-> Item-> delete();
} catch(Exception $ e){
$ error ='您尝试删除的项目与其他记录相关联';
//确切的错误消息是$ e-> getMessage();
$ this-> set('error',$ error);
}


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

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 

How do we deal with this.

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

but i dont want to do a count check.

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

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

Thankz.

解决方案

You could also use 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天全站免登陆