带有MS Server分页的Doctrine2和不在ZF2下工作的命令

时间:2015-12-14 18:24:16

标签: php sql-server doctrine-orm pagination zend-framework2

我有一个按发布日期排序的新闻列表,我想对其进行分页。我在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代码可以运行。任何帮助赞赏。

2 个答案:

答案 0 :(得分:1)

我实际上有一个PR可以解决这个问题。我会看看我是否可以戳它。

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

答案 1 :(得分:0)

我使用了wschalle的PR并创建了我自己的Mssql Driver包含修复程序,也许它可以帮助你。

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