Is there an easy way to check for duplicate keys with Doctrine 2 before doing a flush?
You can catch the UniqueConstraintViolationException
as such:
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
// ...
try {
// ...
$em->flush();
}
catch (UniqueConstraintViolationException $e) {
// ....
}