我有一个非常简单的函数来检查的实体是否存在捆绑:捕获不同的异常类型
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)
所以我看到这个问题是有不同的异常抛出,但我怎么能返回假的任何一个部分不存在的情况?有没有一种“一般”的方式来捕捉symfony中的异常,因为catch (Exception $e)
与use Symfony\Component\Config\Definition\Exception\Exception;
没有捕捉到它。
可能重复[在一个catch块中捕获多个异常类型](https://stackoverflow.com/questions/8439581/catching-multiple-exception-types-in-one-catch-block) – jkucharovic
你真的不'不想依赖于这种东西的例外。看看Doctrine的元数据。特别是:$ em-> getClassMetadata($ entityClassName); – Cerad