[DDC-1241] Additional parameters for EntityRepository::findAll() and EntityRepository::findOneBy() Created: 01/Jul/11  Updated: 23/Aug/13  Resolved: 06/Nov/12

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: 2.1
Fix Version/s: 2.3.1
Security Level: All

Type: Improvement Priority: Trivial
Reporter: Jasper N. Brouwer Assignee: Benjamin Eberlei
Resolution: Fixed Votes: 1
Labels: None


In the Doctrine 2.1 versions the method EntityRepository::findBy() accepts additional parameters for ordering, limit and offset.
Great feature! (While waiting for it I extended the EntityRepository class and implemented these parameters myself using a QueryBuilder.)

It would be nice for the method EntityRepository::findOneBy() to accept an additional parameter for ordering as well. It could use the following signature:
public function findOneBy(array $criteria, array $orderBy = null);
This would be useful for various cases, for example: finding the newest blog-post.

And maybe update EntityRepository::findAll() as well with the signature:
public function findAll(array $orderBy = null);

Looking forward to your thoughts.

Comment by Dieter Provoost [ 27/Jul/11 ]

I can't seem to find the implementation of this even though this issue has been resolved. Is this possible?

Comment by Benjamin Eberlei [ 27/Jul/11 ]

It was only added to "findBy"

Comment by Jasper N. Brouwer [ 27/Jul/11 ]

So the resolution is actually a "won't fix" (in stead of "fixed")?

I know this was added to "findBy", but my request was to extend the "findOneBy" and "findAll" as well...

Comment by Dieter Provoost [ 27/Jul/11 ]

Can it also be added to "findOneBy"? It would come in very handy for the use case above (for example: finding the newest blog-post).
Otherwise, I think we are limited to the alternative using "findBy" with the orderBy parameter (thus fetching all entries, followed by returning only the first/last) or using a native query.

Comment by Benjamin Eberlei [ 27/Jul/11 ]

Reopened for 2.2 with implementation for "findOneby()"

Comment by Guilherme Blanco [ 20/Dec/11 ]

Updating fix version

Comment by PaweĊ‚ Nowak [ 05/Nov/12 ]

A proposed implementation of findOneBy with ordering is available with the following pull request: https://github.com/doctrine/doctrine2/pull/504.

Comment by Fabio B. Silva [ 06/Nov/12 ]

Fixed : https://github.com/doctrine/doctrine2/commit/283ed55824b7c713611e4b1e336551be805c48b5

Comment by Dmitry Pashkevich [ 23/Aug/13 ]

What about extending the signature of EntityRepository::findAll() to this?
public function findAll(array $orderBy = null);

I like this idea, it would allow cleaner code when querying records without criteria:
instead of
$users->findBy(array(), array('name'=>'ASC'));

Generated at Wed Nov 25 09:18:04 EST 2015 using JIRA 6.4.10#64025-sha1:5b8b74079161cd76a20ab66dda52747ee6701bd6.