35

I have the following query:

   $latestcontent = $em->createQuery('
            SELECT c.title, c.content, c.lastedit, a.firstname, a.surname
            FROM ShoutMainBundle:Content c, ShoutMainBundle:Admin a
            WHERE c.author = a.id
            ORDER BY c.lastedit ASC'
            );

What I need to do, is limit the amount of records returned from this query. However, when I add LIMIT 10 to the SQL query, it returns this error:

Error: Expected end of string, got 'LIMIT'.

So, I had a look and found that you could do add ->limit(10) to the code (after the query). But this then throws up this PHP error:

Fatal error: Call to undefined method Doctrine\ORM\Query::limit() in C:\wamp\www\src\Shout\AdminBundle\Controller\DefaultController.php on line 22

What am I doing wrong?

Raffael
  • 19,547
  • 15
  • 82
  • 160
mickburkejnr
  • 3,652
  • 12
  • 76
  • 109

1 Answers1

71

There is no statement like LIMIT for DQL currently, as far as I know.

You have to use Query::setMaxResults().

Community
  • 1
  • 1
Raffael
  • 19,547
  • 15
  • 82
  • 160
  • That's worked, thank you. It's confused me a slight bit as on this page it says there is a limit command (which was what i was using): http://www.doctrine-project.org/documentation/manual/1_1/hu/dql-doctrine-query-language. But your way works, thank you :) – mickburkejnr Sep 13 '11 at 15:16
  • @mickburkejnr: The link you provided in your comment is for Doctrine 1.2. Symfony2 is using Doctrine 2.1, so the documentation you read doesn't match the version you are using. This [link](http://www.doctrine-project.org/projects/orm/2.1/docs/hu) is for the Doctrine 2.1 documentation. – Matt Sep 13 '11 at 19:32
  • 1
    @matt is right, the link I provided was 1.1 (I didn't know). This is the one thing that annoys me most about the new Symfony. The documentation is all over the place. – mickburkejnr Sep 14 '11 at 15:04
  • Link for Doctrine 2.6: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/query-builder.html#limiting-the-result (when using QueryBuilder) – Simon Groenewolt May 02 '18 at 09:55