Doctrine 2:按字段分组(错误:'...'不指向类。) [英] Doctrine 2: group by field alias (Error: '...' does not point to a Class. )
问题描述
我得到这个Doctrine查询:
I got this Doctrine query:
select upper(substring(e.name, 1, 1)) first_letter
from Application\Models\Exercise e
group by first_letter
order by first_letter asc
但是它会引发一个异常:消息:
But it throws an exception with the message:
Error: 'first_letter' does not point to a Class.
如果我通过省略
订单由
,它的工作原理。
If I leave out the group by
and the order by
, it works.
在这种情况下,我必须使用本地查询,或者在客户端代码中进行排序和分组(根据数据量的大小可能不是一个好主意)数据库...)或者是否可以让这个查询工作?
Do I have to use a native query in this case or do the sorting and grouping in my client code (probably not such a good idea depending on the amount of data in the db...) or is it possible to get this query working?
谢谢!
编辑:
这是我目前的做法,不是很好,但是暂时工作,因为没有太多的数据db:
This is my current approach, not so nice, but works for the moment as there is not much data in the db:
$tmpResult = $this->getEntityManager()->createQuery('
select upper(substring(e.name, 1, 1)) first_letter
from Application\Models\Exercise e
')->getResult();
$groupedAndSortedResult = array();
foreach($tmpResult as $row) {
$groupedAndSortedResult[$row['first_letter']] = $row['first_letter'];
}
sort($groupedAndSortedResult);
return array_values($groupedAndSortedResult);
推荐答案
我知道你自己回答了这个问题,想知道你是否尝试使用 DISTINCT
:
I know you've answered this question yourself but just wondering if you tried using DISTINCT
:
select DISTINCT upper(substring(e.name, 1, 1)) first_letter
from Application\Models\Exercise e
order by e.name asc
订购 e.name
相当于 first_letter
在这种情况下。
Ordering by e.name
is equivalent to first_letter
in this case.
这篇关于Doctrine 2:按字段分组(错误:'...'不指向类。)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!