Skip to content


DDC-950: Selecting entities in random order #5479

doctrinebot opened this Issue · 7 comments

4 participants


Jira issue originally created by user enumag:

I need some way to select a random row from a table. There was something like "ORDER BY RAND()" in SQL but I can't really find anything like that for Doctrine.


Comment created by @beberlei:

Doctrine allows you to execute SQL and hydrate objects from that using the Native Query API as described in the documentation.


Issue was closed with resolution "Won't Fix"


Comment created by darkangel:

I need this as well ... why can't we do something like:


Or maybe something at a lower level, like:

$qb->add('orderBy', new Expr\Func('RAND', array()));



Comment created by petersvp:

I need this as well! ORDER BY RAND() or something like this is a MUST HAVE for Doctrine! I do NOT want to fetch ALL then Hydrate ALL to select some rows, nor I want to do 2 queries in a row! Also, last one method actually can return 2 SAME records....

Hope I see this soon. I'm using Symfony 2


Comment created by daslicht:

+1 Please add this feature

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot closed this

I see this is closed, but this is labelled as a new feature... this meaning this will not happen, or this will be built as a new feature... I really want this, since i agree that any real way of doing this is either by multiple actions, always retrieving more records then wanted, or having to go back to writing queries.. no biggy just not what i want.

It seems pretty default to (ie) only need one random record out of millions, especially if i want to do any testing agains integrity...


Is there a compelling reason to make this "integrated" as opposed to a custom DQL method or a native query?

Anything that boils down to ORDER BY RAND() LIMIT 1 sounds like it could cause some unanticipated performance problems in some engines, if they go through the effort of generating a random number for every row and then sorting them all before trimming off the rest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.