First of all I would like to thank stackoverflow community for all the help that has been provided to me the past years ! I usually find a solution, but this time and after hours and hours of research and tests, I can't get any fix. So here is my problem :
On the top of my page I have a menu that is generated from a DB with categories. Here is a part of my code:
CategoryController :
public function renderMenuAction() {
$em = $this->getDoctrine()->getManager();
$manager = new CategoryManager($em);
$request = $this->getRequest();
$categories = $manager->loadMenu();
foreach($categories as $category) {
$this->getLocalizedCategoryTranslations($category, $request);
}
return $this->render('HeidanCoreBundle:Includes/Category:category_menu.html.twig', array(
'categories' => $categories,
));
}
The function loadMenu from my manager refers to this request, to get the lvl0 categories :
CategoryRepository :
public function findMenuElements() {
$qb = $this->_em->createQueryBuilder();
$qb->select('category', 'translation')
->from('HeidanCoreBundle:Category', 'category')
->leftJoin('category.translations', 'translation')
->where('category.lvl = 0')
;
return $query = $qb->getQuery()
->getResult();
}
getLocalizedCategoryTranslations call a listener which set translation values of the category (title, description etc...)
public function getLocalizedCategory(TranslatableEvent $event)
{
$object = $event->getObject();
$locale = $event->getLocale();
if($object instanceof TranslatableInterface) {
if(get_class($object) == 'Heidan\CoreBundle\Entity\Category') {
$this->localizeObject($object, $locale);
$this->localizeCategoryChildren($object, $locale);
}
}
}
Here is my problem : This code is working without any problem on any page, except on those which has a custom request for an element.
IndexController
public function indexAction()
{
$news = $this->getNews();
$announcements = $this->getAnnouncements();
return $this->render('HeidanCoreBundle:Public:index.html.twig', array(
'news' => $news,
'announcements' => $announcements,
));
}
public function getNews() {
$em = $this->getDoctrine()->getManager();
$qb = $em->createQueryBuilder();
$qb->select('article')
->from('HeidanCoreBundle:Article', 'article')
->orderBy('article.createdAt', 'DESC')
->setMaxResults(4);
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
}
Here is a description of my menu.
Admin page (no custom queries like the getNews above - all of these objects are categories):
--Article
----Society
----Business
--Artwork
----Books
----Songs
--Artists
----Authors
----Singers
Index page (there is a getNews query which request the articles):
--No Translation
----No Translation
----No Translation
--Artwork
----Books
----Songs
--Artists
----Authors
----Singers
As you can see, the object translation is not shown on the Index Page. I checked the Symfony Debug Mode, and it's not requested at all. (no select translation query for categories with ID 1, 13, 14 to get the translations whereas for other categories queries are done).
I tried several stuff:
if I remove getNews() and I replace it by null, it's working on Index Page.
if I add $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true); at the end of my getNews function : it's working too on the Index Page !
if I replace my function getNews by getArtist (according the menu), the same problem occurs for the Artist menu categories which are set as 'No Translation'
I think it's a Lazy Loading issue from Doctrine but maybe there is something that I have not understood.
I'm waiting for your help if you have any ideas ! Thanks in advance !