When deleting an entity, I often check manually if the entity is not linked to others before allowing removing it:
public function deleteAction(Operation $operation)
{
if ($operation->getArticles()->isEmpty() && $operation->getMessages()->isEmpty()) {
$em = $this->getDoctrine()->getManager();
$em->remove($operation);
$em->flush();
$this->addFlash('success', sprintf("Operation deleted", $operation));
} else {
$this->addFlash('danger', sprintf("Can't delete operation.", $operation));
}
return $this->redirectToRoute('operation');
}
But this method implies knowing all the relations between the entity and others and I have to check for each one.
Using try...catch
instead is sometimes useful, but the deletion not always raises an exception.
Is there another (more generic) way to check for existence of references to my entity before deleting it?