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
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.");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With