尝试在symfony3.3语法上捕获orm学说

问题描述 投票:0回答:1

我试图通过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']);
}

提前致谢

php try-catch symfony-3.3
1个回答
1
投票

最后我得到了我的问题的解决方案,我想与你分享解决方案

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

此链接包含Doctrine中的所有异常

https://github.com/doctrine/dbal/tree/master/lib/Doctrine/DBAL/Exception

© www.soinside.com 2019 - 2024. All rights reserved.