学说查询构建器限制和偏移 [英] doctrine querybuilder limit and offset

查看:19
本文介绍了学说查询构建器限制和偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个 symfony 初学者,我想用这个框架制作一个博客.我使用存储库通过这种方法获取家庭文章:

i'm a symfony beginner and i want to make a blog with the framework. i use repository to get home articles with this method :

public function getHomeArticles($offset = null, $limit = null)
{
    $qb = $this->createQueryBuilder('a')
               ->leftJoin('a.comments', 'c')
               ->addSelect('c')
               ->addOrderBy('a.created', 'DESC');


    if (false === is_null($offset))
        $qb->setFirstResult($offset);

    if (false === is_null($limit))
        $qb->setMaxResults($limit);

    return $qb->getQuery()
              ->getResult();
}

所以在我的数据库中我有 10 篇文章.在我的 BlogController 中,我使用:

so in my database i have 10 articles. In my BlogController i use :

$blog = $em->getRepository('TestBlogBundle:Article')
                ->getHomeArticles(3,4);

有了这个,我想要 4 篇文章.但作为回报,我也有一篇文章.

With this i want 4 articles. But in return i also have one article.

有什么问题?

推荐答案

这是一个知道问题,如果您的查询包含 fetch-joined 集合,则需要小心使用 setFirstResult()setMaxResults().

This is a know issue where setFirstResult() and setMaxResults() need to be use with care if your query contains a fetch-joined collection.

如关于 第一个和最大结果项:

如果您的查询包含指定结果的 fetch-joined 集合限制方法没有像您期望的那样工作.设置最大结果限制数据库结果行的数量,但是在fetch-joined 集合 一个根实体可能出现在多行中,有效保湿少于指定数量的结果.

If your query contains a fetch-joined collection specifying the result limit methods are not working as you would expect. Set Max Results restricts the number of database result rows, however in the case of fetch-joined collections one root entity might appear in many rows, effectively hydrating less than the specified number of results.

相反,您可以:

  1. 延迟加载

  1. Lazy load

使用分页器(如@Marco 此处所述)

use the Paginator (as stated by @Marco here)

使用 DoctrineCommonCollectionsCollection::slice()

这篇关于学说查询构建器限制和偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆