【发布时间】:2016-03-20 08:17:29
【问题描述】:
我有一个按发布日期排序的新闻列表,我想对其进行分页。我在 Zend Framework 2 项目中使用 Doctrine 2.5。这是我的实体:
<?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;
}
我正在使用Doctrine\DBAL\Driver\SQLSrv\Driver 和 ZF2 的 Paginator 组件。这是分页代码:
$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);
但是当我转到列表时,我得到了错误:
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.
当我删除->orderBy('news.datePublished', 'desc') 行时,我得到了列表,但自然没有排序。这是 Doctrine 生成的 SQL 的一部分:
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
使用 MySQL,代码可以工作。任何帮助表示赞赏。
【问题讨论】:
标签: php sql-server doctrine-orm pagination zend-framework2