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

Closed
opened 2026-01-22 14:39:42 +01:00 by admin · 8 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:42 +01:00
admin closed this issue 2026-01-22 14:39:42 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jan 20, 2015):

Comment created by @ocramius:

We actually want to reduce the repository API, not expand it...

@doctrinebot commented on GitHub (Jan 20, 2015): Comment created by @ocramius: We actually want to reduce the repository API, not expand it...
Author
Owner

@doctrinebot commented on GitHub (Jan 21, 2015):

Comment created by dominikd100:

Wow, ok. I guess you've got good reasons for it and I shouldn't try to convince you, that having methods for most common use cases out of the box is quite convenient?

If yes, then fine. I can create a common EntityRepository subclass for myself and put any helper methods I want there. This would at least cut down on the code duplication in my codebase.

If what you say is final, then we can close this ticket.

Cheers.

@doctrinebot commented on GitHub (Jan 21, 2015): Comment created by dominikd100: Wow, ok. I guess you've got good reasons for it and I shouldn't try to convince you, that having methods for most common use cases out of the box is quite convenient? If yes, then fine. I can create a common EntityRepository subclass for myself and put any helper methods I want there. This would at least cut down on the code duplication in my codebase. If what you say is final, then we can close this ticket. Cheers.
Author
Owner

@doctrinebot commented on GitHub (Jan 22, 2015):

Comment created by @ocramius:

{quote}I guess you've got good reasons for it and I shouldn't try to convince you, that having methods for most common use cases out of the box is quite convenient?{quote}

The main problem is that adding API methods to repositories forces any subclasses to also apply eventual filtering logic to those as well.

Other than that, it's merely a question of interface segregation: the current API is hardly maintainable if we assume that many developers subclassed the EntityRepository class, therefore we should keep it locked until 3.x.

I'll actually mark this issue for 3.0 and defer discussion till then.

@doctrinebot commented on GitHub (Jan 22, 2015): Comment created by @ocramius: {quote}I guess you've got good reasons for it and I shouldn't try to convince you, that having methods for most common use cases out of the box is quite convenient?{quote} The main problem is that adding API methods to repositories forces any subclasses to also apply eventual filtering logic to those as well. Other than that, it's merely a question of interface segregation: the current API is hardly maintainable if we assume that many developers subclassed the `EntityRepository` class, therefore we should keep it locked until 3.x. I'll actually mark this issue for 3.0 and defer discussion till then.
Author
Owner

@doctrinebot commented on GitHub (Jan 22, 2015):

Comment created by dominikd100:

Ok, thanks.

@doctrinebot commented on GitHub (Jan 22, 2015): Comment created by dominikd100: Ok, thanks.
Author
Owner

@ianfp commented on GitHub (Jan 24, 2016):

Perhaps you should consider changing ObjectManager#find() to throw a NoResultException if no entity with the given id exists. (In 3.0, obviously, since this would be a backwards-incompatible change.) This is the behaviour I want 90% of the time, and I believe returning null when you don't have to is considered bad OOP practice, no?

What do you think? Am I totally out to lunch here?

@ianfp commented on GitHub (Jan 24, 2016): Perhaps you should consider changing `ObjectManager#find()` to throw a `NoResultException` if no entity with the given id exists. (In 3.0, obviously, since this would be a backwards-incompatible change.) This is the behaviour I want 90% of the time, and I believe returning null when you don't have to is considered bad OOP practice, no? What do you think? Am I totally out to lunch here?
Author
Owner

@Ocramius commented on GitHub (Jan 24, 2016):

@ianfp I agree, but it's not the general case scenario. find means "look for", therefore what you want is something like ObjectManager#get(), for example ;-)

@Ocramius commented on GitHub (Jan 24, 2016): @ianfp I agree, but it's not the general case scenario. `find` means "look for", therefore what you want is something like `ObjectManager#get()`, for example ;-)
Author
Owner

@ianfp commented on GitHub (Jan 25, 2016):

Yes! And while I understand your desire not to expand the API, ObjectManager#get() would be really handy, and would not be a BC-break...

:-D

@ianfp commented on GitHub (Jan 25, 2016): Yes! And while I understand your desire not to expand the API, `ObjectManager#get()` would be really handy, and would not be a BC-break... :-D
Author
Owner

@guilhermeblanco commented on GitHub (Jan 4, 2017):

The entire API is up for discussion for 3.0. This topic is already in the list to be revisited and decided.
Closing this for now.

@guilhermeblanco commented on GitHub (Jan 4, 2017): The entire API is up for discussion for 3.0. This topic is already in the list to be revisited and decided. Closing this for now.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4344