I am using the Translatable trait from KnpLabs/DoctrineBehaviors with the "proxy translations" for my Category entity, so I have a CategoryTranslation entity containing the property "name", when I want to get the list of all my categories I end up with the Symfony debug toolbar telling me the page has run thousands of queries and took over 5 seconds to load.
This is my code:
$categories = $this->getDoctrine()
->getManager()
->getRepository('OylexCategoryBundle:Category')
->findAll();
$categoryList = array();
foreach ($categories as $category) {
$categoryList[] = array(
'id' => $category->getId(),
'name' => $category->getName(),
);
}
//To output as json
You see, the line 'name' => $category->getName(),
is making a new query for each iteration of the foreach loop.
Is there a way to join the translation table so that the "name" property is fetch alongside the category?
I tried this, but it's still doing a query per call:
$categories = $this->getDoctrine()
->getManager()
->getRepository('OylexCategoryBundle:Category')
->createQueryBuilder('c')
->select(array('c'))
->leftJoin('OylexCategoryBundle:CategoryTranslation', 'ct', 'WITH', 'c.id = ct.translatable AND ct.locale = \'en\'')
->getQuery()
->getResult();
Thanks,
诀窍是使用可翻译模块给出的映射字段进行连接:
->innerJoin('c.translations', 'ct')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.