A 2-step way to get a random item from a select-query:

1. a query to count the number of results:

$query = $em->createQuery('SELECT COUNT(w.wordId) FROM Application\Entity\Word 
w JOIN etc... WHERE etc...GROUP BY w.wordId';$count = 
$query->getSingleScalarResult();

2. use this count to retrieve a random record from the same selection:
$dql = "SELECT w etc... FROM Application\Entity\Word w JOIN etc... WHERE 
etc...GROUP BY w.wordId";$query = $entityManager->createQuery($dql)
                       ->setFirstResult(rand(0, $count - 1))
                       ->setMaxResults(1)->getSingleResult();

It is not a very efficient query, especially the GROUP BY, plus you have to 
query twice, but it is all in DQL and don't need any custom functions.

If you would have several random selections from the same result-set, then it 
might be most efficient to just read the whole result in memory and work from 
there. 
That was also what I had in mind  in my previous answer.

Also see 
http://stackoverflow.com/questions/8601356/doctrine-2-randomly-selecting-a-row-offset-0-or-1-indexed
 
and 
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/pagination.html

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to