Doctrine 2 使用关联映射进行分页 [英] Doctrine 2 Pagination with Association Mapping

查看:21
本文介绍了Doctrine 2 使用关联映射进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何对从 Doctrine 2 中的实体关联映射获得的结果进行分页?例如

class Customer {
  /**
   * @OneToMany(targetEntity="Order")
   */
  private $orders;
}

可以这样使用:

$customer->getOrders();

这将返回 Order 对象的集合.

which will return a collection of Order objects.

问题是当有大量订单对象时.

The problem is when there are a large number of order objects.

我们可以在构建自定义查询时使用 DoctrineORMToolsPaginationPaginator,但是在使用关联映射时我看不到任何方法可以挂钩查询生成.

We can use DoctrineORMToolsPaginationPaginator when building custom queries, however I do not see any way to hook into query generation when utilising association mapping.

class Paginator {
  /** 
   * @param Query|QueryBuilder $query A Doctrine ORM query or query builder. 
   */
  function __construct(
    //....

推荐答案

如果使用EXTRA_LAZY fetch 模式,Doctrine 将不会在水合集合时检索所有对象.它只会在您对集合使用 slice() 方法时检索所需的子集.

If you use the EXTRA_LAZY fetch mode, Doctrine will not retrieve all objects when hydrating the collection. It will only retrieve the subset needed when you use the slice() method on the collection.

class Customer {
    /**
     * @OneToMany(targetEntity="Order", fetch="EXTRA_LAZY")
     */
    private $orders;
}

$cust = new Customer;
$orders = $cust->getOrders()->slice(100, 50);

您需要验证它如何与分页交互.

You would need to verify how this interacts with the pagination.

这篇关于Doctrine 2 使用关联映射进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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