2017-12-20 4 views
0

Ich versuche Lehre Ausnahme in der Steuerung durch Try-Catch Catch, ich bin mit Symfony 3try catch orm Lehre auf symfony3.3 Syntax

try { 
    $em = $this->get('doctrine.orm.entity_manager'); 
    $em->persist($transaction); 
    $em->flush(); 
} catch(Exception $e) { 
    return new JsonResponse(['error' => 'already exist']); 
} 

Vielen Dank im Voraus

+0

... und was ist los? Was * passiert * nicht? Was willst du passieren? – iainn

+0

Ich möchte die Ausnahme abfangen, die passieren wird: EntityAlreadyExist, UniqueConstraint, der Code, den ich auf meiner Frage hinter mir habe, funktioniert nicht für mich –

Antwort

0

Finaly bekam ich die Lösung für mein Problem und ich möchte mit Ihnen die Lösung teilen

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

Dieser Link alle Exception in Lehre

enthält

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