Doctrine2 ORM select for update

前端 未结 2 1453
悲哀的现实
悲哀的现实 2020-12-15 20:59

Could you suggest an approach how to implement SELECT FOR UPDATE with Doctrine?

I need to read a counter value, then use it in PHP code and immediately i

2条回答
  •  长情又很酷
    2020-12-15 21:30

    Locking support

    Doctrine 2 implements Locking support for entities:

    find('User', $theEntityId, LockMode::OPTIMISTIC, $expectedVersion);
    
        // do the work
    
        $em->flush();
    } catch(OptimisticLockException $e) {
        echo "Someone else has already changed this entity. Apply the changes again!";
    }
    

    Native sql

    Also, you can do it throws execute raw SQL:

    $em->getConnection()->exec('LOCK TABLES table_name WRITE;'); //lock for write access
    

    and then

    $em->getConnection()->exec('UNLOCK TABLES;');
    

提交回复
热议问题