Quite often you may encouter a race condition when an entry already exists in
the database and inserting another one ucase unique constraint failure.
Dbal's converts this database error into
UniqueConstraintViolationException
, so you may catch it and
mitigate it.
In the example bellow, the persist may fail because there is already a like for specific author & article.
try {
$like = new Like();
$like->article = $article;
$like->author = $author;
$this->orm->likes->persistAndFlush($like);
} catch (\Nextras\Dbal\UniqueConstraintViolationException $e) {
$this->orm->likes->getMapper()->rollback();
$this->orm->refreshAll()
}