My application uses the Doctrine2 EntityManager. In a loop I like to insert 5 data sets to different tables.
public function insertRate($dateObj, $rate)
{
$model = new TestModel();
$model
->setDate($dateObj)
->setRate($rate);
$this->getEntityManager()->persist($model);
try {
$this->getEntityManager()->flush();
} catch (\Exception $e) {
$currentTime = new \DateTime();
echo 'Duplicate entry <br>' .
'At Time ' . $currentTime->format('Y-m-d H:i') . "<hr>";
}
}
If maybe insert 2 faild because of an duplicated entry (unique constraint) the following 3 statements will not be executed by the entityManger because the manger is closed.
I use a workaround like this:
$currentEm = $this->em;
if (!$currentEm->isOpen()) {
$this->em = $currentEm->create(
$currentEm->getConnection(), $currentEm->getConfiguration());
}
With this workarround, a new entityManger will be set up but the auto_increment ids are not like 1, 2, 3... but 1, 5, 10, 15...
I dont want to check if there can be a unique constraint violation because of the additional selects.
How can I fix this, and is this workarround ok?