getReference caches proxy #5065

Closed
opened 2026-01-22 14:57:34 +01:00 by admin · 4 comments
Owner

Originally created by @fabioginzel on GitHub (Mar 25, 2016).

Originally assigned to: @Ocramius on GitHub.

Example:

  $usuario = $this->em->getReference('Application\Entity\Usuario', $facebookId);

 $usuario = $this->usuarioRepository->find($notExistsId);
  if ($usuario)
     echo "found";

Always return "found" even if the entity does not exist, because it returns the cached proxy

Originally created by @fabioginzel on GitHub (Mar 25, 2016). Originally assigned to: @Ocramius on GitHub. Example: ``` php $usuario = $this->em->getReference('Application\Entity\Usuario', $facebookId); $usuario = $this->usuarioRepository->find($notExistsId); if ($usuario) echo "found"; ``` Always return "found" even if the entity does not exist, because it returns the cached proxy
admin added the BugInvalid labels 2026-01-22 14:57:34 +01:00
admin closed this issue 2026-01-22 14:57:34 +01:00
Author
Owner

@Ocramius commented on GitHub (Mar 25, 2016):

That's expected behavior: what kind of behavior were you expecting, and why?

@Ocramius commented on GitHub (Mar 25, 2016): That's expected behavior: what kind of behavior were you expecting, and why?
Author
Owner

@fabioginzel commented on GitHub (Mar 25, 2016):

I have expected that since $notExistsId doesnt exist it would return null

@fabioginzel commented on GitHub (Mar 25, 2016): I have expected that since $notExistsId doesnt exist it would return null
Author
Owner

@Ocramius commented on GitHub (Mar 25, 2016):

That means that you could potentially have two instances of the same Application\Entity\Usuario inside your UnitOfWork, which breaks one of the base invariants of the ORM

@Ocramius commented on GitHub (Mar 25, 2016): That means that you could potentially have two instances of the same `Application\Entity\Usuario` inside your `UnitOfWork`, which breaks one of the base invariants of the ORM
Author
Owner

@Ocramius commented on GitHub (Mar 25, 2016):

Note that getReference should only ever be used if you are 100% sure that the record will exist, but you do not want to load it immediately

@Ocramius commented on GitHub (Mar 25, 2016): Note that `getReference` should only ever be used if you are 100% sure that the record will exist, but you do not want to load it immediately
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5065