【发布时间】:2018-01-04 11:08:12
【问题描述】:
我有一个非常简单的函数来检查一个实体是否存在于一个包中:
public function checkExists($bundle, $val)
{
try{
$this->em->getRepository($bundle.':'.$val);
}catch (MappingException $e){
return false;
}
return true;
}
所以我有以下几种情况:
Input | Expected | Actual
'AppBundle', 'Company' | true | true
'AppBundle', 'NONEXISTANT' | false | false (MappingException caught)
'NONEXISTANT', 'Company' | false | 500 (ORMException not caught)
'NONEXISTANT', 'NONEXISTANT' | false | 500 (ORMException not caught)
所以我看到问题是抛出了不同的异常,但是对于一个部分不存在的任何一种情况,我怎么能返回 false 呢?在 symfony 中是否有一种“通用”方法来捕获异常,因为 catch (Exception $e) 和 use Symfony\Component\Config\Definition\Exception\Exception; 无法捕获它。
【问题讨论】:
-
你真的不想依赖例外来处理这类事情。看看 Doctrine 的元数据。特别是:$em->getClassMetadata($entityClassName);
标签: php symfony exception-handling try-catch