Doctrine查询构建器 - 按别名分组

时间:2016-06-27 15:20:06

标签: php sql-server doctrine-orm group-by doctrine

我已使用查询构建器完成此查询:

$qb = $this->createQueryBuilder ( 'x' );
$qb->innerJoin ( 'x.unitelement', 'ue', 'WITH', 'ue.id=x.unitelement' );
$qb->innerJoin ( 'ue.object', 'o', 'WITH', 'o.id=ue.object' );
$qb->select ( 'o.id, o.name, AVG(x.evaluation) AS vi, YEAR(x.date) as yyyy' );
$qb->where('x.evaluation IS NOT NULL');
$qb->andWhere('x.evaluation >= 0');
$qb->andWhere('x.date IS NOT NULL');
$qb->groupBy ( 'o.id, o.name, x.date' );
$qb->orderBy('yyyy');
$query = $qb->getQuery ();

我需要按'yyyy'别名对结果进行分组,但如果我将yyyy别名插入groupBy,我就会收到错误。

有人有解决方案在Doctrine Query Builder中转换这个sql:

SELECT o.name, AVG(x.Evaluation) AS vi, YEAR(x.[Date]) AS yr
FROM [Inspection] AS x
INNER JOIN [UnitElement] AS ue
ON ue.id=x.UnitElement_ID
INNER JOIN Object AS o
ON o.id=ue.[Object_ID]
WHERE x.evaluation IS NOT NULL
AND x.evaluation >= 0
AND x.[Date] IS NOT NULL
GROUP BY o.id, o.name, YEAR(x.[Date])
ORDER BY vi DESC;

由于

1 个答案:

答案 0 :(得分:0)

尝试

$qb = $this->createQueryBuilder()
    ->from('Inspection', 'x')
    ->select('o.id, o.name, AVG(x.evaluation) AS vi, YEAR(x.date) as yyyy' )
    ->join('UnitElement', 'ue', Join::WITH, 'ue.id = x.UnitElement_ID')
    ->join('Object', 'o', Join::WITH, 'o.id = ue.Object_ID')
    ->where('x.evaluation IS NOT NULL AND x.evaluation >= 0 AND x.Date IS NOT NULL')
    ->orderBy('vi', 'DESC')
    ->groupBy('o.id, o.name, yyyy');