我一直试图拉出一个随机行,我用过这个:
这是我发现并没有真正帮助的示例代码(我在这里找到:https ://gist.github.com/pierroweb/1518601 )
class QuestionRepository extends EntityRepository
{
public function findOneRandom()
{
$em = $this->getEntityManager();
$max = $em->createQuery('
SELECT MAX(q.id) FROM EnzimQuestionBundle:Question q
')
->getSingleScalarResult();
return $em->createQuery('
SELECT q FROM EnzimQuestionBundle:Question q
WHERE q.id >= :rand
ORDER BY q.id ASC
')
->setParameter('rand',rand(0,$max))
->setMaxResults(1)
->getSingleResult();
}
}
现在我有这样的事情:
$em = $this->getEntityManager();
$max = $em->createQuery('SELECT MAX(p.id) FROM GreenMonkeyDevGlassShopBundle:Product p')->getSingleScalarResult();
return $em->createQuery('SELECT p FROM GreenMonkeyDevGlassShopBundle:Product p INNER JOIN (SELECT p2.categories. FROM GreenMonkeyDevGlassShopBundle:Product p.categories WHERE :cid IN(pc) p.id >= :rand ORDER BY p.id ASC')
->setParameter('cid', $category_id)
->setParameter('rand',rand(0,$max))
->setMaxResults(intval($limit))
->getSingleResult();
我不断收到此错误:
FatalErrorException: Error: Call to undefined method Doctrine\ORM\Query\ResultSetMapping::addRootEntityFromClassMetadata() in /var/www/gmd-milkywayglass/src/GreenMonkeyDev/GlassShopBundle/Entity/CategoryRepository.php line 42
关于我做错了什么有什么想法吗?我知道 Doctrine 没有获取随机方法。也许有一些简单的解决方案?谢谢!