Doctrine2 与 MS Server 分页和排序不在 ZF2 下工作

Doctrine2 with MS Server pagination and order by not working under ZF2

我有一个按发布日期排序的新闻列表,我想对其进行分页。我在 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 的分页器组件。这是分页代码:

    $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 代码有效。任何帮助表示赞赏。

实际上我有一个公开的 PR 来解决这个问题。我会看看我是否可以把它戳进去。

https://github.com/doctrine/dbal/pull/818

我使用了 wschalle 的 PR 并创建了我自己的 Mssql 驱动程序,其中包含修复程序,也许它对你有帮助。

https://github.com/kokspflanze/GameBackend/tree/master/src/GameBackend/DBAL