Symfony2 - 在 $em->remove() 上捕获异常

Symfony2 - Catch Exception on $em->remove()

我试图在执行时捕获异常:

try {
    $em->remove($education);
    $em->flush();
} catch(PDOException $e) {
    var_dump($e->getMessage());
    die;
}

我也尝试了 \Exception\Doctrine\ORM\ORMException,但其中 none 成功了。

我没有转储异常消息,而是得到了同样的错误,我试图避免捕获异常:

[3/3] ForeignKeyConstraintViolationException: An exception occurred while executing 'DELETE FROM trainee_education WHERE id = ?' with params [2]:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (trainingexperience.internship, CONSTRAINT FK_10D1B00C2CA1BD71 FOREIGN KEY (education_id) REFERENCES trainee_education (id))

我知道为什么我的约束失败了,没什么新鲜事。但是我想得到一个例外,这样我就可以通知用户他不能删除一个对象,因为他在创建关系之前使用过它。

我也有同样的情况,但是我和\Doctrine\DBAL\DBALException

一起工作
try{

      $em->persist($question);
      $em->flush();
} catch (\Doctrine\DBAL\DBALException $e) {

      $exception_message = $e->getPrevious()->getCode();
    return $this->render('AppBundle:Errors:error.html.twig', array('error' => $exception_message)); 

} 

我不知道这是否是最佳解决方案,但可行。