Note that there are some explanatory texts on larger screens.

plurals
  1. POSelecting random DB entry in Symfony2 - Getting an error
    text
    copied!<p>I have been trying to pull out a random row, I have used this: </p> <p>This is the example code I found which did not really help ( I found here: <a href="https://gist.github.com/pierroweb/1518601" rel="nofollow">https://gist.github.com/pierroweb/1518601</a>)</p> <pre><code>class QuestionRepository extends EntityRepository { public function findOneRandom() { $em = $this-&gt;getEntityManager(); $max = $em-&gt;createQuery(' SELECT MAX(q.id) FROM EnzimQuestionBundle:Question q ') -&gt;getSingleScalarResult(); return $em-&gt;createQuery(' SELECT q FROM EnzimQuestionBundle:Question q WHERE q.id &gt;= :rand ORDER BY q.id ASC ') -&gt;setParameter('rand',rand(0,$max)) -&gt;setMaxResults(1) -&gt;getSingleResult(); } } </code></pre> <p>Now I have something like this:</p> <pre><code> $em = $this-&gt;getEntityManager(); $max = $em-&gt;createQuery('SELECT MAX(p.id) FROM GreenMonkeyDevGlassShopBundle:Product p')-&gt;getSingleScalarResult(); return $em-&gt;createQuery('SELECT p FROM GreenMonkeyDevGlassShopBundle:Product p INNER JOIN (SELECT p2.categories. FROM GreenMonkeyDevGlassShopBundle:Product p.categories WHERE :cid IN(pc) p.id &gt;= :rand ORDER BY p.id ASC') -&gt;setParameter('cid', $category_id) -&gt;setParameter('rand',rand(0,$max)) -&gt;setMaxResults(intval($limit)) -&gt;getSingleResult(); </code></pre> <p>I keep getting this error though: <code>FatalErrorException: Error: Call to undefined method Doctrine\ORM\Query\ResultSetMapping::addRootEntityFromClassMetadata() in /var/www/gmd-milkywayglass/src/GreenMonkeyDev/GlassShopBundle/Entity/CategoryRepository.php line 42</code></p> <p>Any thoughts on what I can be doing wrong? I know that Doctrine does not have a get random method. Maybe there is some simple solution? Thank you!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload