\Doctrine\ORM\EntityManager::find() with optimistic lock for deleted entity raises an error #6705

Closed
opened 2026-01-22 15:37:20 +01:00 by admin · 0 comments
Owner

Originally created by @ilianiv on GitHub (Apr 29, 2021).

If the requested entity ID does not exist (e.g. because it has been deleted), the error is triggered.

Version: 2.8.4

Error:
Doctrine\ORM\UnitOfWork::lock(): Argument 1 ($entity) must be of type object, null given

Called in:
vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:474

public function find($className, $id, $lockMode = null, $lockVersion = null)
    {
       ...
       $entity = $unitOfWork->tryGetById($sortedId, $class->rootEntityName);
       ...
        switch (true) {
            case $lockMode === LockMode::OPTIMISTIC:
                $entity = $persister->load($sortedId);   // This may return NULL

>>>>>           $unitOfWork->lock($entity, $lockMode, $lockVersion);  

                return $entity;

            case $lockMode === LockMode::PESSIMISTIC_READ:
            case $lockMode === LockMode::PESSIMISTIC_WRITE:
                return $persister->load($sortedId, null, null, [], $lockMode);

            default:
                return $persister->loadById($sortedId);
        }
    }
Originally created by @ilianiv on GitHub (Apr 29, 2021). If the requested entity ID does not exist (e.g. because it has been deleted), the error is triggered. **Version:** 2.8.4 **Error:** Doctrine\ORM\UnitOfWork::lock(): Argument 1 ($entity) must be of type object, null given **Called in:** vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:474 ``` public function find($className, $id, $lockMode = null, $lockVersion = null) { ... $entity = $unitOfWork->tryGetById($sortedId, $class->rootEntityName); ... switch (true) { case $lockMode === LockMode::OPTIMISTIC: $entity = $persister->load($sortedId); // This may return NULL >>>>> $unitOfWork->lock($entity, $lockMode, $lockVersion); return $entity; case $lockMode === LockMode::PESSIMISTIC_READ: case $lockMode === LockMode::PESSIMISTIC_WRITE: return $persister->load($sortedId, null, null, [], $lockMode); default: return $persister->loadById($sortedId); } } ```
admin added the Bug label 2026-01-22 15:37:20 +01:00
admin closed this issue 2026-01-22 15:37:20 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6705