I have a list of news ordered by the published date and I want to paginate it. I am using Doctrine 2.5 in a Zend Framework 2 project. Here's my entity:
<?php
namespace BuscadorJuridico\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation as Form;
/**
* Class News
* @package BuscadorJuridico\Entity
*
* @ORM\Table(name="news")
* @ORM\Entity
*/
class News
{
/**
* @var int
*
* @ORM\Column(name="news_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="news_title", type="string", length=200, nullable=false)
*/
public $title;
/**
* @var string
*
* @ORM\Column(name="news_body", type="blob", nullable=false)
*
*/
public $body;
/**
* @var \DateTime
*
* @ORM\Column(name="news_date_published", type="date", nullable=true)
*
*/
protected $datePublished;
/**
* @var bool
*
* @ORM\Column(name="news_status", type="boolean", nullable=false)
*
*/
public $published;
}
I am using the Doctrine\DBAL\Driver\SQLSrv\Driver
and the Paginator componenr of ZF2. Here is the pagination code:
$query = $this
->entityManager
->getRepository('BuscadorJuridico\Entity\News')
->createQueryBuilder('news')
->select()
->orderBy('news.datePublished', 'desc')
->andWhere('news.published = true');
$paginator = new Paginator(new DoctrineAdapter(new ORMPaginator($query)));
$paginator->setCurrentPageNumber($page);
$paginator->setItemCountPerPage($count);
But when I go to the list, I get the error:
SQLSTATE [42000, 8120]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Column 'dctrn_result.news_date_published_3' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
When I delete the ->orderBy('news.datePublished', 'desc')
line I get the list, but naturally not ordered. This is part of the SQL that Doctrine is generating:
SELECT DISTINCT
news_id_0,
MIN(sclr_5) AS dctrn_minrownum,
ROW_NUMBER() OVER (ORDER BY news_date_published_3 DESC) AS doctrine_rownum
FROM (
SELECT n0_.news_id AS news_id_0,
n0_.news_title AS news_title_1,
n0_.news_body AS news_body_2,
n0_.news_date_published AS news_date_published_3,
n0_.news_status AS news_status_4,
ROW_NUMBER() OVER(ORDER BY n0_.news_date_published DESC) AS sclr_5
FROM news n0_
WHERE n0_.news_status = 1)
dctrn_result
GROUP BY news_id_0
With MySQL the code works. Any help appreciated.