Hi,
I am using below code to capture “SQLSTATE 23503: Foreign key violation” error code to display friendly message to end user. Displaying friendly message & everything working fine except it will generate entry under application.log of Yii. It’s:
2012/07/10 19:07:05 [error] [system.db.CDbCommand] CDbCommand::execute() failed: SQLSTATE[23503]: Foreign key violation: 7 ERROR: update or delete on table "posts" violates foreign key constraint "fk_comments_posts" on table "comments"
DETAIL: Key (id)=(3041) is still referenced from table "comments".. The SQL statement executed was: DELETE FROM "posts" WHERE "posts"."id"='3041'.
My application code:
try {
if ($post->delete()) {
Yii::app()->user->setFlash('success', Yii::t('post', 'The Post has been successfully deleted.'));
$success = true;
} else
Yii::app()->user->setFlash('error', Yii::t('post', $post->getLastError()));
} catch (Exception $e) {
if ($e->getCode() == 23503) {
Yii::app()->user->setFlash('error', Yii::t('post', 'The Post record cannot be deleted. Post already in use.'));
$success = true;
} else
Yii::app()->user->setFlash('error', Yii::t('post', $e->getMessage()));
}
if ($success)
echo 'success';
else
echo 'failed';
Could anybody point me what is the mistake I am doing or is this the default behavior of PHP exception handling process? Because I thought if I handling the exception it won’t produce any entries under logs.
Thank you,
Yohan