Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show an alert message on delete of parent node in symfony2 instead of exception page? [closed]

hi every one im new to symfony2

I have a user entity having relation of one to many with services

and service has one to one relation with email service and newsletter service.

i want to show an alert message on delete parent node instead of

exception page. e.g user jhon having services of web and newsletter on delete

of user jhon i want show an alert message instead of this

 An exception occurred while executing 'DELETE FROM user WHERE id = ?' with 
 params ["21"]:

   SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or 
   update   a parent row: a foreign key constraint fails 
 (`mwanmobile_bi`.`service`, CONSTRAINT `FK_E19D9AD2A76ED395` FOREIGN KEY 
  (`user_id`) REFERENCES `user` (`id`))

Kindly guide me accordingly thanks in advance

like image 280
Zargham irtza Avatar asked Oct 28 '15 09:10

Zargham irtza


1 Answers

You can catch a ForeignKeyConstraintViolationException exception and display a flash message to the user.

use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;

// Action
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('AppBundle:User')->find($id);

try {
    $em->remove($user);
    $em->flush();

    $this->addFlash('success', 'User removed');
} catch (ForeignKeyConstraintViolationException $e) {
    $this->addFlash('error', "This user has connected services, so it can't be removed.");
}
like image 93
lsouza Avatar answered Oct 22 '22 01:10

lsouza