2学说与分页关联映射 [英] Doctrine 2 Pagination with Association Mapping

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

问题描述

我想知道的你怎么能在分页学说从实体关联映射得到的结果2?例如

 类客户{
  / **
   * @OneToMany(targetEntity =订单)
   * /
  私人$订单;
}

可以被用作这样:

  $以客户为> getOrders();

这将返回订单对象的集合。

问题是,当有大量的订单对象。

我们可以使用学说\\ ORM \\ TOOLS \\分页\\分页程序创建自定义查询的时候,但是我没有看到任何方式使用关联映射时挂接到查询生成。

 类分页程序{
  / **
   * @参数查询| QueryBuilder的$查询学说ORM查询或查询生成器。
   * /
  功能__construct(
    // ....


解决方案

如果您使用EXTRA_LAZY抓取模式,补水的集合时学说将检索所有对象。当您使用slice()方法的集合,将只检索所需要的子集。

 类客户{
    / **
     * @OneToMany(targetEntity =订单,取=EXTRA_LAZY)
     * /
    私人$订单;
}$卡斯特=新的客户;
$订单= $ cust-> getOrders() - >片(100,50);

您需要验证该如何与分页进行交互。

I am wondering how can you paginate the results obtained from an entity association mapping in Doctrine 2? For example

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

can be used as such:

$customer->getOrders();

which will return a collection of Order objects.

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

We can use Doctrine\ORM\Tools\Pagination\Paginator 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(
    //....

解决方案

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.

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

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