我是Doctrine的新手,我开始将它用于个人项目。我正在使用laravel-doctrine库来使用laravel。
不幸的是,当我尝试订购分页查询时,我看到以下错误:
An exception occurred while executing 'SELECT DISTINCT id_0 FROM (SELECT
t0_.id AS id_0, t0_.title AS title_1, t0_.slug AS slug_2, t0_.views AS
views_3, t0_.replies AS replies_4, t0_.created_at AS created_at_5,
t0_.updated_at AS updated_at_6, p1_.id AS id_7, p1_.body AS body_8,
p1_.created_at AS created_at_9, p1_.updated_at AS updated_at_10,
p1_.deleted_at AS deleted_at_11, c2_.id AS id_12, c2_.name AS name_13,
c2_.slug AS slug_14, c2_.created_at AS created_at_15, c2_.updated_at AS
updated_at_16 FROM topics t0_ LEFT JOIN posts p1_ ON t0_.id = p1_.topic_id
LEFT JOIN categories c2_ ON t0_.category_id = c2_.id) dctrn_result ORDER BY
created_at_5 ASC LIMIT 10 OFFSET 0':
SQLSTATE[HY000]: General error: 3065 Expression #1 of ORDER BY clause is not
in SELECT list, references column 'dctrn_result.created_at_5' which is not
in SELECT list; this is incompatible with DISTINCT
导致此错误的实际代码如下:
public function findAll($results = 10, $pageName = 'page') {
$query = $this->createQueryBuilder('t')
->select('t', 'f', 'c')
->leftJoin('t.first', 'f', 'ON')
->leftJoin('t.category', 'c', 'ON')
->orderBy('t.createdAt', 'asc')
->getQuery();
return $this->paginate($query, $results, $pageName);
}
我正在尝试选择一个主题列表,其中的类别和第一个帖子都是按主题created_at日期排序的。
我花了一些时间研究这个问题,但是到目前为止还没有弄清楚发生了什么,我还有另一种方法吗?或者我的代码是不正确的?
答案 0 :(得分:2)
事实证明,paginate()
的第四个参数导致此问题,默认为true
,但将其设置为false
会导致此问题重新开始。
public function paginate(Query $query, $perPage, $pageName = 'page', $fetchJoinCollection = true)
只需将$fetchJoinCollection
设为false即可。我不确定究竟为什么,但它现在有效。
答案 1 :(得分:0)
根据这个http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/pagination.html原则,计算正确计数的步骤为3步:
我认为当ORDER BY子句附加在子查询之外而不是内部时会出现错误,这会产生SQL错误。在文档的底部,他们确实说设置fetchJoinCollection = false将跳过1步。这可能会解决这个问题。
我希望他们在将来的版本中解决这个问题。