Doctrine2 ORM 选择更新

Doctrine2 ORM select for update(Doctrine2 ORM 选择更新)
本文介绍了Doctrine2 ORM 选择更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否建议一种如何使用 Doctrine 实现 SELECT FOR UPDATE 的方法?

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

我需要读取一个计数器值,然后在 PHP 代码中使用它,并在其他人(来自另一个进程)使用相同的值之前立即递增该值.

I need to read a counter value, then use it in PHP code and immediately increment the value before someone else (from another process) uses the same value.

推荐答案

锁定支持

Doctrine 2 实现 对实体的锁定支持:

<?php
use DoctrineDBALLockMode;
use DoctrineORMOptimisticLockException;

$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!";
}

本机 sql

另外,你可以抛出执行原始 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 选择更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)