我正在DB中实现订阅。电子邮件必须是唯一的,所以我在数据库中有一个UNIQUE索引。我有这样的代码在我的页面初始化:在db中处理重复错误的正确方法是什么
$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邮件,这是检测的误差是重复的条目的唯一途径?
请看http://fossies.org/unix/www/atk4-4.1.zip:a/atk4/lib/SQLException.php。恕我直言,我认为你可以一般处理它们。报告错误已经足够了。或者,您可以先检查用户电子邮件是否存在。 – Tommy
在询问这里之前,我查看了源代码......在重复的情况下是不够的,我需要在另一个页面上重定向用户。也许SQLException可以正确设置错误代码以反映数据库错误或将其映射为更多数据库不可知的方式? –
@TheElter伟大的问题。 – romaninsh