querybuilder选择总和并加入实体

时间:2017-12-05 08:49:15

标签: symfony doctrine-orm query-builder

我正在尝试选择总和和完整的连接实体

$qb = $this->createQueryBuilder('trl')
        ->select('sum(trl.billable_amount) as billable_amount,
          sum(trl.billable_duration) as billable_duration, 
          r as resource')
        ->join('trl.time_report', 'tr')
        ->join('trl.contact', 'c')
        ->leftJoin('c.resource', 'r')
        ->where('tr.id = :id')
        ->setParameter('id', $timeReportId)
        ->groupBy('r.id')
        ->having('billable_amount > 0');

错误我得到:[语义错误]第0行,第1行附近'SELECT sum(trl.billable_amount)':错误:如果不选择至少一个根实体别名,则无法通过标识变量选择实体。

没有“r as resource”就可以正常工作。是否可以像这样加入,或者我应该只获得资源ID并获得第二个查询的实体?

1 个答案:

答案 0 :(得分:0)

也选择root实体别名,试试这个

$qb = $this->createQueryBuilder('trl')
    ->select('trl, sum(trl.billable_amount) as billable_amount,
      sum(trl.billable_duration) as billable_duration, 
      r as resource')