我有一个具有一对多关系的类,我想检索按关系成员数排序的实体。
Here我已经部分找到了解决方案。但问题是我只得到结果中的第一个实体。任何帮助都非常感谢!
这是代码:
//from the AdminCause.php
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="Vote", mappedBy="cause")
*/
protected $votes;
//the corresponding repository
class AdminCauseRepository extends EntityRepository
{
public function findAllOrderedByVotesNumber()
{
return $this->getEntityManager()
->createQuery('SELECT x, COUNT(v) as HIDDEN votes FROM AreweUserBundle:AdminCause x LEFT JOIN x.votes v ORDER BY votes')
->getResult();
}
}
//and them call it in the controller
$causes = $em->getRepository('AreweUserBundle:AdminCause')->findAllOrderedByVotesNumber();