Country の複数の Doctrine Entities インスタンスを含む ArrayCollection があると想像してください。また、私のメソッド 'getName()' が、実際には複数の言語との 1 対多の関係であると想像してください (A2lixTranslation サポート)...
要点は、この ArrayCollection が ElasticSearch サービスから構築されていることです。したがって、これらすべてのエンティティを取得し、反復して名前を出力すると、すべてのカテゴリに対する追加のクエリにすぎません。
この状況を管理する方法がよくわからないので、150 の余分なクエリを維持することはできません...
実装例
$countries = // ArrayCollection of Countries, returned by any mapping system.
foreach ($countries as $country) {
/**
* As name is an entity, uses lazy loading in every iteration
* Because I get collection as it comes, I would like to retrieve
* all names in one query. I thought about perform a DQL with a join
* of all countries and their names, so Doctrine will catch'em all
* but only catch my query and results, and do not identify retrieved
* results with my collection, so is not working...
*/
$name = $country->getName();
echo $name;
}
// Could be nice do something like this...
$countries = // ArrayCollection of Countries, returned by any mapping system.
$queryBuilder = $this
->getDoctrine()
->getRepository('ProjectCoreBundle:Country')
->createQueryBuilder('c');
/**
* This query result should only add Cache with results
*/
$queryBuilder
->select('c','t')
->innerJoin('c.countryName','cn','WITH','c.id = cn.Country')
->getQuery()
->getResult();
foreach ($countries as $country) {
/**
* At this poing, as Name relation entity is already loaded and cached
* lazy load will simply return object ( Any query is performed )
*/
$name = $country->getName();
echo $name;
}