Pergunta

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!

Foi útil?

Solução 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'));
    });

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top