使用Symfony / Doctrine,检查一个实体是否被另一个实体引用

时间:2019-03-11 14:41:21

标签: symfony doctrine-orm doctrine

在删除实体时,我经常手动检查该实体是否未链接到其他实体,然后再将其删除:

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');
}

但是这种方法意味着知道实体与其他实体之间的所有关系,因此我必须检查每个实体。 有时使用try...catch很有用,但是删除并不总是引发异常。
在删除实体之前,还有另一种(更通用的)方式来检查是否存在对我实体的引用吗?

0 个答案:

没有答案
相关问题