教义2按顺序排序,加入Symfony 2 Pagerfanta

时间:2014-07-02 09:21:17

标签: php mysql symfony doctrine-orm

我正在使用Symfony2和Doctrine 2,我正在为Symfony 2的Pagerfanta Bundle创建一个查询。

查询如下:

$qb = $this->createQueryBuilder('t')
            ->select()
            ->join('t.pois', 'p');
$qb->groupBy('t.id');
$qb->addOrderBy('count(case when p.image = 1 then 1 else null end)', 'DESC');

将提供以下查询:

SELECT t FROM Bundle\Entity\Turn t INNER JOIN t.pois p GROUP BY t.id ORDER BY count(case when p.image = 1 then 1 else null end) DESC

我首先创建了一个MySQL查询,所以我可以在Doctrine 2中重建它,如下所示:

SELECT *
FROM `turn`
LEFT JOIN (
    poi
) ON ( turn.id = poi.turn_id )
GROUP BY turn.id
ORDER BY count( case when poi.image = 1 then 1 else null end) DESC;

这个原生查询工作得很好,在phpmyadmin中测试。

但是,Doctrine 2 Query提出了这个例子:

Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("[Syntax Error] line 0, col 115: Error: Expected end of string, got '('") in Bundle:Admin:showTurnsFiltered.html.twig at line 64.

QueryException: [Syntax Error] line 0, col 115: Error: Expected end of string, got '(' 

我做错了什么?或者Pagerfanta软件包无法使用此查询?

1 个答案:

答案 0 :(得分:0)

我通过向turn实体(count_poi)添加一列并通过本机SQL填充它来修复此问题。

之后,查询如下:

SELECT *
FROM `turn`
ORDER BY t.count_poi DESC;

但这只是一种解决方法,我对它不满意,但现在可以使用