DDC-2106: Notice trying to match a Criteria referencing a managed entity not yet persisted #2652

Closed
opened 2026-01-22 13:59:23 +01:00 by admin · 6 comments
Owner

Originally created by @doctrinebot on GitHub (Oct 29, 2012).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user frosas:

Undefined index: (...) in Doctrine/ORM/UnitOfWork.php line 2735

#0 doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(2735): Symfony\Component\HttpKernel\Debug\ErrorHandler->handle(8, 'Undefined index...', '/Users/frosas/S...', 2735, Array) 
#1 doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php(1696): Doctrine\ORM\UnitOfWork->getEntityIdentifier(Object(MyEntity)) 
#2 doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php(1683): Doctrine\ORM\Persisters\BasicEntityPersister->getIndividualValue(Object(MyEntity)) 
#3 doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php(818): Doctrine\ORM\Persisters\BasicEntityPersister->getValue(Object(MyEntity)) 
#4 doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php(786): Doctrine\ORM\Persisters\BasicEntityPersister->expandCriteriaParameters(Object(Doctrine\Common\Collections\Criteria)) 
#5 doctrine/orm/lib/Doctrine/ORM/PersistentCollection.php(835): Doctrine\ORM\Persisters\BasicEntityPersister->loadCriteria(Object(Doctrine\Common\Collections\Criteria)) 
#6 (...): Doctrine\ORM\PersistentCollection->matching(Object(Doctrine\Common\Collections\Criteria)) 
#7 (...)

As a workaround I had to control this case explicitly:

if ($myEntity->getId() === null) return null;
$criteria = Criteria::create()->where(Criteria::expr()->eq('field', $myEntity));
return $this->collection->matching($criteria)->first();
Originally created by @doctrinebot on GitHub (Oct 29, 2012). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user frosas: ``` Undefined index: (...) in Doctrine/ORM/UnitOfWork.php line 2735 #0 doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(2735): Symfony\Component\HttpKernel\Debug\ErrorHandler->handle(8, 'Undefined index...', '/Users/frosas/S...', 2735, Array) #1 doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php(1696): Doctrine\ORM\UnitOfWork->getEntityIdentifier(Object(MyEntity)) #2 doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php(1683): Doctrine\ORM\Persisters\BasicEntityPersister->getIndividualValue(Object(MyEntity)) #3 doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php(818): Doctrine\ORM\Persisters\BasicEntityPersister->getValue(Object(MyEntity)) #4 doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php(786): Doctrine\ORM\Persisters\BasicEntityPersister->expandCriteriaParameters(Object(Doctrine\Common\Collections\Criteria)) #5 doctrine/orm/lib/Doctrine/ORM/PersistentCollection.php(835): Doctrine\ORM\Persisters\BasicEntityPersister->loadCriteria(Object(Doctrine\Common\Collections\Criteria)) #6 (...): Doctrine\ORM\PersistentCollection->matching(Object(Doctrine\Common\Collections\Criteria)) #7 (...) ``` As a workaround I had to control this case explicitly: ``` if ($myEntity->getId() === null) return null; $criteria = Criteria::create()->where(Criteria::expr()->eq('field', $myEntity)); return $this->collection->matching($criteria)->first(); ```
admin added the Bug label 2026-01-22 13:59:23 +01:00
admin closed this issue 2026-01-22 13:59:24 +01:00
Author
Owner

@doctrinebot commented on GitHub (Feb 9, 2013):

Comment created by @asm89:

Can you provide us with the mapping of your entities? The code shouldn't get to the point where it asks for the identifier of the object while the object isn't persisted yet.

@doctrinebot commented on GitHub (Feb 9, 2013): Comment created by @asm89: Can you provide us with the mapping of your entities? The code shouldn't get to the point where it asks for the identifier of the object while the object isn't persisted yet.
Author
Owner

@doctrinebot commented on GitHub (Feb 10, 2013):

Comment created by frosas:

I'm not sure mapping can help to solve this and is quite a work for me to get it without the client details. Anyway I'll try to reproduce it in a custom scenario if you require it.

I've been doing some debugging and I got this:

  • It still fails with last 2.3.*@stable
  • Entity is persisted before matching(). At UnitOfWork::persistNew(), $this->entityStates[$oid] is set to self::STATE_MANAGED but $this->entityIdentifiers is untouched ($idGen->isPostInsertGenerator() is true)
  • When BasicEntityProvider::getIndividualValue() is called, $value state is STATE_MANAGED but $uow->getEntityIdentifier() fails because $uow->entityIdentifiers[$oid] was never set.

Hope it helps.

@doctrinebot commented on GitHub (Feb 10, 2013): Comment created by frosas: I'm not sure mapping can help to solve this and is quite a work for me to get it without the client details. Anyway I'll try to reproduce it in a custom scenario if you require it. I've been doing some debugging and I got this: - It still fails with last 2.3.*@stable - Entity is persisted before matching(). At UnitOfWork::persistNew(), $this->entityStates[$oid] is set to self::STATE_MANAGED but $this->entityIdentifiers is untouched ($idGen->isPostInsertGenerator() is true) - When BasicEntityProvider::getIndividualValue() is called, $value state is STATE_MANAGED but $uow->getEntityIdentifier() fails because $uow->entityIdentifiers[$oid] was never set. Hope it helps.
Author
Owner

@doctrinebot commented on GitHub (Feb 10, 2013):

Comment created by @asm89:

If you can create a testcase and send a PR with it that would be very helpful. You can create it and put in in the appropriate class here:
https://github.com/doctrine/doctrine2/tree/master/tests/Doctrine/Tests/ORM/Functional/Ticket

@doctrinebot commented on GitHub (Feb 10, 2013): Comment created by @asm89: If you can create a testcase and send a PR with it that would be very helpful. You can create it and put in in the appropriate class here: https://github.com/doctrine/doctrine2/tree/master/tests/Doctrine/Tests/ORM/Functional/Ticket
Author
Owner

@doctrinebot commented on GitHub (Feb 16, 2013):

Comment created by frosas:

Here you have: https://github.com/doctrine/doctrine2/pull/582

@doctrinebot commented on GitHub (Feb 16, 2013): Comment created by frosas: Here you have: https://github.com/doctrine/doctrine2/pull/582
Author
Owner

@doctrinebot commented on GitHub (May 1, 2013):

Comment created by @beberlei:

Fixed in 2.4

@doctrinebot commented on GitHub (May 1, 2013): Comment created by @beberlei: Fixed in 2.4
Author
Owner

@doctrinebot commented on GitHub (May 1, 2013):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (May 1, 2013): Issue was closed with resolution "Fixed"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2652