DDC-3527: Provide a method to retrieve exactly one entity out of entity repository #4343

Open
opened 2026-01-22 14:39:40 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Jan 20, 2015).

Originally assigned to: @Ocramius, @guilhermeblanco on GitHub.

Jira issue originally created by user dominikd100:

Hello,

currently we have EntityRepository::findOneBy() method for retrieving a single or none entities out of an EntityRepository. It would be very useful, if there was a method which retrieves exactly one entity or throws NonUniqueResultException in case of multiple records returned or NoResultException in case of zero results returned (just like Query::getSingleResult() currently does).

Very often I find myself creating custom repositories for a couple of ::findOneByOrThrow() methods only. Needless to say it's quite mundane to do. The bodies of such methods follows the same pattern:

  1. Query the EntityRepository using EntityRepository::findOneBy().
  2. If the result is null, then throw an Exception.
  3. Return the result.

It'd be very nice to have it out-of-the-box in the base EntityRepository.

Thanks.

Originally created by @doctrinebot on GitHub (Jan 20, 2015). Originally assigned to: @Ocramius, @guilhermeblanco on GitHub. Jira issue originally created by user dominikd100: Hello, currently we have EntityRepository::findOneBy() method for retrieving a single or none entities out of an EntityRepository. It would be very useful, if there was a method which retrieves exactly one entity or throws NonUniqueResultException in case of multiple records returned or NoResultException in case of zero results returned (just like Query::getSingleResult() currently does). Very often I find myself creating custom repositories for a couple of ::findOneByOrThrow() methods only. Needless to say it's quite mundane to do. The bodies of such methods follows the same pattern: 1. Query the EntityRepository using EntityRepository::findOneBy(). 2. If the result is null, then throw an Exception. 3. Return the result. It'd be very nice to have it out-of-the-box in the base EntityRepository. Thanks.
admin added the Improvement label 2026-01-22 14:39:40 +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#4343