Symfony2教义GROUP BY [英] Symfony2 doctrine GROUP BY

查看:153
本文介绍了Symfony2教义GROUP BY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想通过这些查询对GROUP BY结果进行分组:

  SELECT MONTH(p.publicationDateStart)AS归档
FROM ApplicationSonataNewsBundle:发布p
GROUP BY归档

但它给我错误:

 在渲染模板期间抛出异常
(Notice:Undefined index:archive in
/home/kiddo/Code/apache2/work/ibctal.dev/vendor/doctrine/orm/lib/在ApplicationSonataNewsBundle中发布:archive.html.twig

我如何解决这个问题。



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



编辑:



这是控制器代码



命名空间应用程序\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归档,
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版本

解决方案

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




MONTH(p.publicationDateStart)作为存档,
CAST(p.publicationDateStart as date)AS HIDDEN archiveDate
FROM
ApplicationSonataNewsBundle:Post p
GROUP BY
MONTH(archiveDate)


I create a MONTH function to extract month of a date.

I want to GROUP BY the result with these query:

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

But it gives me error:

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. 

How can I fix this problem.

For reference I found this information of doctrine's commit: https://github.com/doctrine/doctrine2/commit/2642daa43851878688d01625f272ff5874cac7b2:

EDIT :

Here is the Controller Code

namespace Application\Sonata\NewsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller; use 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
        ));
    } }

And FYI, I use 2.3.1-DEV version

解决方案

I had to use a similar workaround for this same purpose, check out my solution:

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

这篇关于Symfony2教义GROUP BY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆