I’m implementing a subscription in a DB. The email must be unique, so I have a UNIQUE index in the database. I have this code in my page init:
$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
}
}
The only information in SQLException is a formatted HTML message, is this the only way to detect if the error is from a duplicated entry?
Here is one way to do it:
https://github.com/atk4/atk4-web/blob/master/lib/Model/ATK/User.php#L95
Although if you want to perform custom action on duplication, you should move getBy outside of the model, into page’s logic.