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