dongliao2241 2017-08-03 10:36
浏览 127

EntityManager由唯一键冲突关闭

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?

  • 写回答

1条回答 默认 最新

  • duanquan1243 2017-08-03 11:19
    关注

    After failed transaction, your EntityManager gets locked – it's expected behavior. You can reset it via:

    $this->getDoctrine()->resetManager();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器