Details
Description
For performance reasons (solving the N+1 problem) it might be necessary to load all proxy instances of a given type:
$orders = $em->getRepository('Order')->findAll();
foreach ($orders AS $order) {
$order->getCustomer()->getId(); // n+1
}
However say we could do something like:
$orders = $em->getRepository('Order')->findAll();
$em->getUnitOfWork()->loadProxies("Customer");
foreach ($orders AS $order) {
$order->getCustomer()->getId(); // n+1
}
This doesnt make too much sense in this case because we could use a fetch join with DQL. However you easily get to the point where you have a query like:
SELECT b, e, r, p FROM Bug b JOIN b.engineer e JOIN b.reporter r JOIN b.products p ORDER BY b.created DESC
In this case the SQL generated is VERY huge and just the number of joins could cause an performance overhead. In general its better to keep the number of joins as small as possible, a solution here would optimize for the engineer and reporter being both instances of User:
$bugs = $em->createQuery("SELECT b, p FROM Bug b JOIN b.products p ORDER BY b.created DESC")->getResult(); $em->getUnitOfWork()->loadProxies("User"); // will load all engineers and reporters in one query
We could even make this nicer for using Queries:
$bugs = $em->createQuery("SELECT b, p FROM Bug b JOIN b.products p ORDER BY b.created DESC") ->setQueryHint(Query::LOAD_PROXIES, array("User")) ->getResult();
Implemented, but in another semantical way. Batching of eager loads of FETCH=EAGER associations aswell as AbstactQuery::setFetchMode($className, $assocName, $fetchMode) function to set this case-by-case for DQL.