Dynamically disable Optimistic Locking #7283

Open
opened 2026-01-22 15:49:05 +01:00 by admin · 0 comments
Owner

Originally created by @ilconfo on GitHub (Dec 18, 2023).

Feature Request

Dynamically disable Optimistic Locking

Summary

Hello,
thanks for your hard work, and for this fantastic library.

I've searched a lot but I couldn't find a method to disable optimistic/pessimistic locking. I report at the end of the comment the changes.

The rationale is this: in some projects I almost always want to keep using optimistic locking to protect entities from overwriting data if two users are working on the same entity at the same time.

But sometimes I would like to change some accessory fields in the same entity, for example using a command in a cronjob, and sometimes when I do that the command updates the entity while a user is changing it. So, the user will get an OptimisticLockException.
This is not always desiderable, for instance I could be calculating some field that is updatable only by the command, because the users will only read it (e.g. like a summation of totals linked to the entity).

I could do this using Native SQL, but I've found that changing at this line

/var/www/capitolium/backend/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php:3479

From

if ($bool) {
    $this->requiresFetchAfterChange = true;
}

To
$this->requiresFetchAfterChange = $bool;

It is possible to dynamically disable Optimistic Locking in a command, using:

$cmf = $this->em->getMetadataFactory();
$class = $cmf->getMetadataFor(Invoice::class);  //class to disable
$class->setVersioned(false);
$class->setVersionField(null);

Could be this possibility useful for the library?
Thanks
Claudio

Originally created by @ilconfo on GitHub (Dec 18, 2023). ### Feature Request Dynamically disable Optimistic Locking #### Summary Hello, thanks for your hard work, and for this fantastic library. I've searched a lot but I couldn't find a method to disable optimistic/pessimistic locking. I report at the end of the comment the changes. The rationale is this: in some projects I almost always want to keep using optimistic locking to protect entities from overwriting data if two users are working on the same entity at the same time. But sometimes I would like to change some accessory fields in the same entity, for example using a command in a cronjob, and sometimes when I do that the command updates the entity while a user is changing it. So, the user will get an OptimisticLockException. This is not always desiderable, for instance I could be calculating some field that is updatable only by the command, because the users will only read it (e.g. like a summation of totals linked to the entity). I could do this using Native SQL, but I've found that changing at this line /var/www/capitolium/backend/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php:3479 From ``` if ($bool) { $this->requiresFetchAfterChange = true; } ``` To ` $this->requiresFetchAfterChange = $bool;` It is possible to dynamically disable Optimistic Locking in a command, using: ``` $cmf = $this->em->getMetadataFactory(); $class = $cmf->getMetadataFor(Invoice::class); //class to disable $class->setVersioned(false); $class->setVersionField(null); ``` Could be this possibility useful for the library? Thanks Claudio
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7283