我有两个通过一对多双向关系相关的类。对于每个订阅,将向"订阅"表中添加新行。我想获取按最大订阅数量排序的培训列表。 以下是我的实体:
培训实体:
class Trainings
{
/**
* @ORMOneToMany(targetEntity="AppBundleEntitySubscriptions", mappedBy="id_training")
*/
private $subscriptions_lists;
// ...
}
订阅实体:
class Subscriptions
{
/**
* @ORMManyToOne(targetEntity="AppBundleEntityTrainings" , inversedBy="subscriptions_lists")
* @ORMJoinColumn(name="id_training", referencedColumnName="id",onDelete="CASCADE")
*/
private $id_training;
查询生成器:
$trainings = $em->getRepository('AppBundle:Trainings')
->createQueryBuilder('t')
->innerJoin('t.subscriptions_lists', 'subscription')
->orderBy('COUNT(subscription.id)', 'DESC')
->getQuery()
->getResult();
我收到此异常:
[语法错误] 第 0 行,第 87 行:错误:预期的已知函数,得到"COUNT">
您需要添加一个包含计数值的字段,并在按它排序后
试试这个:
$trainings = $em->getRepository('AppBundle:Trainings')
->createQueryBuilder('t');
$trainings->select('t, COUNT(subscription.id) AS mycount')
->leftJoin('t.subscriptions_lists', 'subscription')
->groupBy('subscription.id_training')
->orderBy('mycount', 'DESC')
->getQuery()
->getResult();