Doctrine 2 Pagination with Association Mapping

坚强是说给别人听的谎言 提交于 2020-01-13 13:50:32

问题


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(
    //....

回答1:


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:


Collections have a filtering API that allows to slice parts of data from a collection. If the collection has not been loaded from the database yet, the filtering API can work on the SQL level to make optimized access to large collections. Doctrine Filtering Collections

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

  public function getOrders($offset = 0, $limit = 30){

    $criteria = Criteria::create()
                ->setFirstResult($offset)
                ->setMaxResults($limit);

    return $this->orders->matching($criteria);

  }

}

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


来源:https://stackoverflow.com/questions/10043588/doctrine-2-pagination-with-association-mapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!