try catch orm doctrine on symfony3.3 syntax

M. Hanafi

I am trying to Catch Doctrine exception in the controller by try catch, i am using Symfony 3

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

Thanks in advance

M. Hanafi

Finaly i got the solution for my problem and i wanna share the solution with you

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

This link contains all Exception in Doctrine

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Symfony3, Missing argument 1 for Doctrine\ORM\EntityRepository::__construct(), how and why

From Dev

Relation ManyToMany with doctrine/symfony3

From Dev

Right join query in symfony3 with doctrine

From Dev

Right join query in symfony3 with doctrine

From Dev

Symfony3 - Using LEFT or RIGHT with doctrine

From Dev

CKEditor convert back to text Symfony 3 and Doctrine ORM

From Dev

update createdAt & updatedAt fields automatically in symfony3 (doctrine)

From Dev

Symfony3 Doctrine findByDate filter by month and/or year

From Dev

Symfony3, Doctrine2, custom fixtures loader

From Dev

Symfony3 Doctrine2 can not access variable in the loop

From Dev

Symfony3 Doctrine, disable PDO emulate_prepares?

From Dev

Symfony3 Doctrine2 can not access variable in the loop

From Dev

symfony3 doctrine2 association counter field

From Dev

Symfony3/Doctrine2 : subquery with InnerJoin using QueryBuilder

From Dev

getAll values from Doctrine entity (Symfony3)

From Dev

symfony 3 Argument 1 passed to [some service]::__construct() must be an instance of [something] instance of Doctrine\ORM\EntityRepository given

From Dev

symfony 3 Argument 1 passed to [some service]::__construct() must be an instance of [something] instance of Doctrine\ORM\EntityRepository given

From Dev

Why Symfony requires doctrine/orm

From Dev

How to Try Catch in symfony

From Dev

How to Try Catch in symfony

From Dev

Codeigniter 3 with Doctrine 2 - Doctrine\ORM\Mapping\MappingException Class is not a valid entity or mapped super class

From Dev

Does Symfony3 / Doctrine open one MySQL connection per visitor?

From Dev

How to getPath of Gedmo Tree element using symfony3, doctrine2 and stofDoctrineExtensionsBundle

From Dev

Image Upload Issue with Symfony3, Doctrine 2 and Sonata Admin Bundle

From Dev

Symfony Doctrine catch connection exception

From Dev

Ajax with Symfony3

From Java

Swift do-try-catch syntax

From Dev

PHP alternative syntax for try-catch

From Dev

Error "The CSRF token is invalid. Please try to resubmit the form" in Symfony3

Related Related

  1. 1

    Symfony3, Missing argument 1 for Doctrine\ORM\EntityRepository::__construct(), how and why

  2. 2

    Relation ManyToMany with doctrine/symfony3

  3. 3

    Right join query in symfony3 with doctrine

  4. 4

    Right join query in symfony3 with doctrine

  5. 5

    Symfony3 - Using LEFT or RIGHT with doctrine

  6. 6

    CKEditor convert back to text Symfony 3 and Doctrine ORM

  7. 7

    update createdAt & updatedAt fields automatically in symfony3 (doctrine)

  8. 8

    Symfony3 Doctrine findByDate filter by month and/or year

  9. 9

    Symfony3, Doctrine2, custom fixtures loader

  10. 10

    Symfony3 Doctrine2 can not access variable in the loop

  11. 11

    Symfony3 Doctrine, disable PDO emulate_prepares?

  12. 12

    Symfony3 Doctrine2 can not access variable in the loop

  13. 13

    symfony3 doctrine2 association counter field

  14. 14

    Symfony3/Doctrine2 : subquery with InnerJoin using QueryBuilder

  15. 15

    getAll values from Doctrine entity (Symfony3)

  16. 16

    symfony 3 Argument 1 passed to [some service]::__construct() must be an instance of [something] instance of Doctrine\ORM\EntityRepository given

  17. 17

    symfony 3 Argument 1 passed to [some service]::__construct() must be an instance of [something] instance of Doctrine\ORM\EntityRepository given

  18. 18

    Why Symfony requires doctrine/orm

  19. 19

    How to Try Catch in symfony

  20. 20

    How to Try Catch in symfony

  21. 21

    Codeigniter 3 with Doctrine 2 - Doctrine\ORM\Mapping\MappingException Class is not a valid entity or mapped super class

  22. 22

    Does Symfony3 / Doctrine open one MySQL connection per visitor?

  23. 23

    How to getPath of Gedmo Tree element using symfony3, doctrine2 and stofDoctrineExtensionsBundle

  24. 24

    Image Upload Issue with Symfony3, Doctrine 2 and Sonata Admin Bundle

  25. 25

    Symfony Doctrine catch connection exception

  26. 26

    Ajax with Symfony3

  27. 27

    Swift do-try-catch syntax

  28. 28

    PHP alternative syntax for try-catch

  29. 29

    Error "The CSRF token is invalid. Please try to resubmit the form" in Symfony3

HotTag

Archive