0
votes

In Symfony 1.4 and Doctrine 1.2 i can:

$bodies = Doctrine::getTable('Body')->createQuery('b')
                        ->where('b.visible = 1')
                        ->orderBy('LENGTH(b.title) ASC')
                        ->execute();

and then working ok.

In Symfony 2 and Doctrine 2 i have:

$bodies = $this->getDoctrine()
                  ->getRepository('MainBodyBundle:Body')
                  ->createQueryBuilder('b')
                  ->where('b.visible = 1')
                  ->orderBy('LENGTH(b.title)', 'ASC')
                  ->getQuery()
                  ->getResults();

but this not working, i have error:

An exception has been thrown during the rendering of a template ("[Syntax Error] line 0, col 114: Error: Expected end of string, got '('") in "MainBodyBundle::index.html.twig".

2
And the what is the error message? - Przemyslaw Kruglej
I think you're missing a question. - Denis

2 Answers

2
votes

If you want to order by string length you should user LENGTH function in select:

 $bodies = $this->getDoctrine()
                  ->getRepository('MainBodyBundle:Body')
                  ->createQueryBuilder('b')
                  ->select('b,LENGTH(b.title) lgth')
                  ->where('b.visible = 1')
                  ->orderBy('lgth', 'ASC')
                  ->getQuery()
                  ->getResults();
0
votes

The error in this case seems to be a syntax error and is coming from the twig file not the repository or controller. So i think the you should first check your index.html.twig for a syntax error rather than checking the query.