1

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 !

2
  • May I ask, if you've been so much on SO, why did you create a fresh account for this question? Commented Aug 20, 2014 at 22:12
  • It is the first time I participate and ask a question here. So this is my first account on stackoverflow. Commented Aug 21, 2014 at 3:00

1 Answer 1

1

I finally managed to find the solution after using debugger. My first though was half right, half wrong. The problem is due to Doctrine behavior but it comes from a line of my code.

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);
        }
    }

}

In this function (I edited the question also to let appear this part of code) I call the get_class method.

When I use the debugger, I found that when I call the getNews() method (which return Article objects), the Category (only for Categories which has Articles) returned is not an object of Heidan\CoreBundle\Entity\Category but Proxies__CG__\Heidan\CoreBundle\Entity\Category

So, this set my condition if(get_class($object) == 'Heidan\CoreBundle\Entity\Category') to false and the Category was not translated

In order to get the correct behavior, I had to inject entity manager to my event and get the class name using the function getClassMetadata

Correct getLocalizedCategory function :

public function getLocalizedCategory(TranslatableEvent $event)
{
    $object = $event->getObject();
    $locale = $event->getLocale();
    $em = $event->getEm();

    if($object instanceof TranslatableInterface) {
        $className = $em->getClassMetadata(get_class($object))->getName();
        if($className == 'Heidan\CoreBundle\Entity\Category') {
            $this->localizeObject($object, $locale);
            $this->localizeCategoryChildren($object, $locale);
        }
    }

}

This link helped me Get entity name from class object

Hope this helps somebody else.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.