学说查询,在查询中用户的排序依据总和

问题描述 投票:1回答:1

我想在谁已完成一定数量的任务,每一个用户的实体执行查询。有与每个任务相关联的不同点的值,我需要创建一个查询,将排序依据那些与他们所有的在$ timeArray如下所示的时间段任务的最高分。

我已经使用SUM查询像这样已经尝试:

- >选择( 'SUM(t.points)') - > ORDERBY( 't.points', 'DESC')

但这只是给出了每个人总积分的数组,我需要返回一个对象,我可以遍历并显示用户在指定时间段的所有任务的最高分。

public function getTimePeriodUsers($userType = null)
{   
    $timeArray = [
        '-1 year', 
        '-3 month', 
        '-1 month', 
        '-1 week',
    ];
    foreach($timeArray as $time){
        $date = new \DateTime();
        $date->modify($time);
        if($userType == null){
              $resultsArray[$time] = $this->createQueryBuilder('u')

                    ->leftJoin('u.userTasks', 'ut')
                    ->leftJoin('ut.task', 't')
                    ->leftJoin('t.challenges', 'ch')     
                    ->andWhere('ut.completed > :date')
                    ->andWhere('u.enabled = :enabled')
                    ->setParameter('enabled', true)
                    ->setParameter('date', $date)
                    ->getQuery()
                    ->getResult();


    }

    return  $resultsArray;

}

sql symfony doctrine-orm doctrine
1个回答
0
投票

您可以使用隐藏的关键字。基本上你可以在SELECT子句中声明一个新的领域,标志着它隐藏避免ORM抓取。

只需添加

->addSelect('SUM(t.points) as HIDDEN score') ->orderBy('score', 'DESC')

在这个文章herehere更多参考

© www.soinside.com 2019 - 2024. All rights reserved.