Doctrine 2 Pagination with Association Mapping
associations, doctrine, doctrine-orm, orm, zend-framework
Solution
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.
Problem
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( //.... ```