首页 > 解决方案 > Symfony Doctrine 事务循环问题

问题描述

我正在尝试根据用户详细信息创建用户并阻止他们的考试编号以下是我的代码,

我正在使用事务来查看考场是否可用,如果不可用则恢复创建用户,

但是回滚也恢复了我维护的日志表以存储分配中的问题。

foreach($user_details as $ky=> $user) {

$this->em->getConnection()->beginTransaction();


try {


    $this->em->persist($user);
    $this->em->flush();


    // this function checks if user details contains some condition
    // for allocation of exam hall,
    // like  fees paid etc
    $examHallAvailable = $this->checkHallavailable($user);

    if($examHallAvailable) {

        // updated exam details for user 
        $user->addExamHall($examHallAvailable);
        $this->em->persist($user);
        $this->em->flush();
        $this->em->getConnection()->commit();   
    } else {

        // i want to rollback creation of this user
        // but continue other user creation
        $this->em->getConnection()->rollBack();
    }

    // this adds a log of user add fail in another table
    $this->updateLog($user);
}catch(Exception $ex) {
    $this->em->getConnection()->rollBack();
}

如果我在没有考场标志的情况下删除回滚,则会出现以下错误

 The optimistic lock on an entity failed.  

我如何继续 for 循环而忽略用户的错误,并在出现错误时防止日志表更新回滚?

标签: symfonytransactionsdoctrine

解决方案


推荐阅读