php - 处理数据库中重复错误的正确方法是什么

标签 php database atk4

我正在数据库中实现订阅。电子邮件必须是唯一的,所以我在数据库中有一个 UNIQUE 索引。我的页面初始化中有这段代码:

$f = $p->add('MVCForm');
$f->setModel('Account',array('name','surname','email'));
$f->elements['Save']->setLabel('Subscribe');

if($f->isSubmitted())
{
    try
    {
         $f->update();

         //More useful code to execute when all is ok :)

    }
    catch(Exception_ValidityCheck $v)
    {
        //Handles validity constraint from the model
        $f->getElement($v->getField())->displayFieldError($v->getMessage());
    }
    catch(SQLException $se)
    {
        //If I'm here there is a problem with the db/query or a duplicate email
    }
}

SQLException 中的唯一信息是格式化的 HTML 消息,这是检测错误是否来自重复条目的唯一方法吗?

最佳答案

这是一种方法:

https://github.com/atk4/atk4-web/blob/master/lib/Model/ATK/User.php#L95

尽管如果您想对复制执行自定义操作,您应该将 getBy 移到模型之外,移到页面的逻辑中。

关于php - 处理数据库中重复错误的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7672423/

相关文章:

PHP。使用 list() 和 every() 吗?

PHP,通过 POST 传递数组

database - 访问音乐基因组数据库

mysql - 在 MySQL 中,我在 my.cnf 中放置了什么,以便所有表都是 UTF-8,默认情况下可以与表情符号一起使用?

javascript - 通过ajax加载页面后初始化atk4 js

jquery - ATK4 - 根据相关下拉列表中的更改更新表单字段值

php - 用于创建成员(member)网站的 Agile Toolkit (ATK4) 文档或教程?

Javascript 生成的输入字段未发布

php - 如何取消两个图像的链接?

sql - Oracle UPDATE 作为 where 子句内的内部查询