0

我创建了一个 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 版本

4

1 回答 1

0

为了同样的目的,我不得不使用类似的解决方法,看看我的解决方案:

SELECT 
    MONTH(p.publicationDateStart) as archive, 
    CAST(p.publicationDateStart as date) AS HIDDEN archiveDate 
FROM
    ApplicationSonataNewsBundle:Post p
GROUP BY 
    MONTH(archiveDate)
于 2013-01-02T13:26:55.953 回答