尝试在 symfony 3.3 语法中捕获 orm 学说
try catch orm doctrine on symfony3.3 syntax
我正在尝试通过 try catch 在控制器中捕获 Doctrine 异常,我正在使用 Symfony 3
try {
$em = $this->get('doctrine.orm.entity_manager');
$em->persist( $transaction );
$em->flush();
} catch(Exception $e) {
return new JsonResponse(['error' => 'already exist']);
}
提前致谢
我终于找到了问题的解决方案,我想与您分享解决方案
try {
$em = $this->get('doctrine.orm.entity_manager');
$em->persist( $transaction );
$em->flush();
} catch(\Doctrine\DBAL\Exception\UniqueConstraintViolationException $e) {
throw new \Symfony\Component\HttpKernel\Exception\HttpException(409, "Transaction already exist" );
} catch(\Doctrine\DBAL\Exception\ConstraintViolationException $e ) {
throw new \Symfony\Component\HttpKernel\Exception\HttpException(409, "Bad request on Transaction" );
} catch(\Doctrine\DBAL\Exception\TableNotFoundException $e ) {
throw new \Symfony\Component\HttpKernel\Exception\HttpException(409, "Transaction Table not found" );
}
这个link包含Doctrine中的所有异常
https://github.com/doctrine/dbal/tree/master/lib/Doctrine/DBAL/Exception
我正在尝试通过 try catch 在控制器中捕获 Doctrine 异常,我正在使用 Symfony 3
try {
$em = $this->get('doctrine.orm.entity_manager');
$em->persist( $transaction );
$em->flush();
} catch(Exception $e) {
return new JsonResponse(['error' => 'already exist']);
}
提前致谢
我终于找到了问题的解决方案,我想与您分享解决方案
try {
$em = $this->get('doctrine.orm.entity_manager');
$em->persist( $transaction );
$em->flush();
} catch(\Doctrine\DBAL\Exception\UniqueConstraintViolationException $e) {
throw new \Symfony\Component\HttpKernel\Exception\HttpException(409, "Transaction already exist" );
} catch(\Doctrine\DBAL\Exception\ConstraintViolationException $e ) {
throw new \Symfony\Component\HttpKernel\Exception\HttpException(409, "Bad request on Transaction" );
} catch(\Doctrine\DBAL\Exception\TableNotFoundException $e ) {
throw new \Symfony\Component\HttpKernel\Exception\HttpException(409, "Transaction Table not found" );
}
这个link包含Doctrine中的所有异常
https://github.com/doctrine/dbal/tree/master/lib/Doctrine/DBAL/Exception