Symfony2学说GROUP BY

时间:2013-01-02 09:21:51

标签: symfony doctrine

我创建一个MONTH函数来提取日期的月份。

我想用这些查询对结果进行GROUP BY:

SELECT MONTH(p.publicationDateStart) AS archive 
FROM ApplicationSonataNewsBundle:Post p 
GROUP BY archive

但它给了我错误:

An exception has been thrown during the rendering of a template 
("Notice: Undefined index: archive in 
/home/kiddo/Code/apache2/work/ibctal.dev/vendor/doctrine/orm/lib/Doctrine/ORM/Query
/SqlWalker.php line 2197") in ApplicationSonataNewsBundle:Post:archive.html.twig 
at line 10. 

如何解决此问题。

作为参考,我发现了教义提交的这些信息: https://github.com/doctrine/doctrine2/commit/2642daa43851878688d01625f272ff5874cac7b2

编辑:

这是控制器代码

命名空间Application \ Sonata \ NewsBundle \ Controller;

使用Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller; 使用Doctrine \ ORM \ Query \ ResultSetMapping;

class SidebarController extends Controller
{
    public function archiveAction()
    {
        $em = $this->getDoctrine()->getManager();
        $dql = "SELECT 
                    MONTH(p.publicationDateStart) AS archive, 
                    p.publicationDate AS HIDDEN archiveDate
                FROM 
                    ApplicationSonataNewsBundle:Post p 
                GROUP BY 
                    MONTH(archiveDate)";
        $query = $em->createQuery($dql);
        $paginator = $this->get('knp_paginator');
        $pager = $paginator->paginate($query, $this->get('request')->query->get('page', 1), 10);

        return $this->render('ApplicationSonataNewsBundle:Sidebar:archive.html.twig', array(
            'archives' => $pager
        ));
    } }

仅供参考,我使用的是2.3.1-DEV版本

1 个答案:

答案 0 :(得分:0)

我必须为此目的使用类似的解决方法,请查看我的解决方案:

SELECT 
    MONTH(p.publicationDateStart) as archive, 
    CAST(p.publicationDateStart as date) AS HIDDEN archiveDate 
FROM
    ApplicationSonataNewsBundle:Post p
GROUP BY 
    MONTH(archiveDate)