gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:36:25 24 4
gpt4 key购买 nike

我正在数据库中实现订阅。电子邮件必须是唯一的,所以我在数据库中有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com