我有一個按發佈日期排序的新聞列表,我想分頁。我在Zend Framework 2項目中使用Doctrine 2.5。這裏是我的實體:Doctrine2與MS服務器分頁和訂單不工作根據ZF2
<?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;
}
我使用ZF2的Doctrine\DBAL\Driver\SQLSrv\Driver
和分頁程序componenr。下面是分頁代碼:
$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的代碼工作。任何幫助讚賞。
@chechopeefe合併! –