문제

I do a query. It is fine. I can list my categories very well. But I do have couple languages. The result from my db is in English. How can I sort the result after I translate in the html.php-template. Or do I have to do it in the action?

Of course my translation in symfony2 looks like this: $this->get('translator')->trans('title.imprint')

Thanks for a hint!

도움이 되었습니까?

해결책 2

I want to answer my own question, so somebody might use it. I use usort and strcmp.

$categories_db = $this->em->getRepository('FrontendContentBundle:CategoriesAllocation')
                        ->findBy(array('state' => $state_id,
                            'module' => $module_id
                        ));

    usort($categories_db, function($a, $b) {
        return strcmp($this->translator->trans($a->getCategory()->getCategory(), array(), 'categories'), $this->translator->trans($b->getCategory()->getCategory(), array(), 'categories'));
    });

다른 팁

You can use twig sort function, which will sort your query like php asort function. Another way is to use TwigExtension. With this you can create your own filter.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top