I have a constraint on a field in a table that the particular field must be unique. If I try and create a new client and use an id that is not unique I generate a CDbException error. I would like this error to be gracefully handled by a view rather than the getting "CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation:…etc" being written to the browser.
In my main config file I have:
'components'=>array(
'errorHandler'=>array(
// use 'site/error' action to display errors
'errorAction'=>'site/error',
In my sitecontroller is
public function actionError()
{
if($error=Yii::app()->errorHandler->error)
{
if ( CDbException == $error->type) {
$this->redirect(array("site/error")); }
else if(Yii::app()->request->isAjaxRequest) {
echo $error['message']; }
else
$this->render('error', $error);
}
}
And I have a view in the …views\site\error.php
How can I have the error displayed in a view rather than the CDbException being written straight to the browser?