Add option to change ProxyFactory behavior when an entity is not found #7191

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

Originally created by @VincentLanglet on GitHub (Jul 31, 2023).

Feature Request

Q A
New Feature yes
RFC yes
BC Break no

Summary

Currently when the ProxyFactory does not found an entity, it throws an exception
https://github.com/doctrine/orm/blob/2.15.x/lib/Doctrine/ORM/Proxy/ProxyFactory.php#L229-L238

In our project, we have a filter enabled by default
https://www.doctrine-project.org/projects/doctrine-orm/en/2.15/reference/filters.html#filters
which ends up sometimes no entity is returned for an id.

A simple use case can be "SoftDelete", the filter adds If foo.deletedAt is not null, which means we can have

$this->userRepository->find(42); // NULL because the user 42 is softDeleted
$post->getAuthor()?->getName(); // Crash because the author is the user 42 and the ProxyFactory throw a not found
$this->userRepository->find($post->getAuthor()->getId()); // NULL which is not consistent with the previous line

But we have also more complex use cases.

We'd like to have

$this->userRepository->find(42); // => NULL because the user 42 is softDeleted
$post->getAuthor()?->getName(); // NULL because the author is the user 42 and the ProxyFactory didn't found the entity too
$this->userRepository->find($post->getAuthor()->getId()); // NULL

Could it be considered to introduce an option and change
https://github.com/doctrine/orm/blob/2.15.x/lib/Doctrine/ORM/Proxy/ProxyFactory.php#L229
to

if ($entity === null && $this->failOnNotFound) {

Thanks.

Originally created by @VincentLanglet on GitHub (Jul 31, 2023). ### Feature Request | Q | A |------------ | ------ | New Feature | yes | RFC | yes | BC Break | no #### Summary Currently when the ProxyFactory does not found an entity, it throws an exception https://github.com/doctrine/orm/blob/2.15.x/lib/Doctrine/ORM/Proxy/ProxyFactory.php#L229-L238 In our project, we have a filter enabled by default https://www.doctrine-project.org/projects/doctrine-orm/en/2.15/reference/filters.html#filters which ends up sometimes no entity is returned for an id. A simple use case can be "SoftDelete", the filter adds `If foo.deletedAt is not null`, which means we can have ``` $this->userRepository->find(42); // NULL because the user 42 is softDeleted $post->getAuthor()?->getName(); // Crash because the author is the user 42 and the ProxyFactory throw a not found $this->userRepository->find($post->getAuthor()->getId()); // NULL which is not consistent with the previous line ``` But we have also more complex use cases. We'd like to have ``` $this->userRepository->find(42); // => NULL because the user 42 is softDeleted $post->getAuthor()?->getName(); // NULL because the author is the user 42 and the ProxyFactory didn't found the entity too $this->userRepository->find($post->getAuthor()->getId()); // NULL ``` Could it be considered to introduce an option and change https://github.com/doctrine/orm/blob/2.15.x/lib/Doctrine/ORM/Proxy/ProxyFactory.php#L229 to ``` if ($entity === null && $this->failOnNotFound) { ``` Thanks.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7191