本文介绍了Doctrine2 ORM选择更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请向我建议一下如何用Doctrine实现 SELECT FOR UPDATE
的方法?
Please could you suggest me some approach how to implement SELECT FOR UPDATE
with Doctrine?
读取一些计数器值,在PHP代码中使用它,并在其他人(从另一个进程)使用相同的值之前立即增加该值。
I need to read some counter value, use it in PHP code and immediately increment the value before someone else (from another process) uses the same value.
推荐答案
锁定支持
Doctrine 2 implements :
<?php
use Doctrine\DBAL\LockMode;
use Doctrine\ORM\OptimisticLockException;
$theEntityId = 1;
$expectedVersion = 184;
try {
$entity = $em->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
另外,你可以执行raw SQL:
Native sql
Also, you can do it throws execute raw SQL:
$em->getConnection()->exec('LOCK TABLES table_name WRITE;'); //lock for write access
然后
$em->getConnection()->exec('UNLOCK TABLES;');
这篇关于Doctrine2 ORM选择更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!