How to show warning message about deleting parent node in symfony2 instead of exception page?

hi every new one for symfony2

I have a one-to-many custom object with services

and the service has a one-to-one relationship with the email service and newsletter.

I want to show warning message about parent node exception instead

page with exceptions. for example, a user who has Internet and newsletter services when deleting

by jhon I want to show a warning message instead

 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`))

I advise you in advance, thanks in advance

+4
source share
1

ForeignKeyConstraintViolationException - .

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.");
}
+4

Source: https://habr.com/ru/post/1613459/


All Articles