Checking for duplicate keys with Doctrine 2

tom picture tom · Oct 19, 2010 · Viewed 41.2k times · Source

Is there an easy way to check for duplicate keys with Doctrine 2 before doing a flush?

Answer

DonCallisto picture DonCallisto · Mar 2, 2016

You can catch the UniqueConstraintViolationException as such:

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;

// ...

try {
   // ...
   $em->flush();
}
catch (UniqueConstraintViolationException $e) {
    // ....
}