Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine LIMIT Syntax Error?

Tags:

mysql

doctrine

'[Syntax Error] line 0, col 71: Error: Expected end of string, got 'LIMIT'' 

Here's my code:

public function getLatestChapters()
    {
        return $this->_em->createQuery('SELECT c, m FROM models\Chapter c JOIN c.Manga m ORDER BY c.CreateDate LIMIT 10')->getResult();
    }

What could posibly the problem for this? How can I use LIMIT in Doctrine?

I am using Doctrine 2

like image 378
DucDigital Avatar asked Jul 09 '11 15:07

DucDigital


2 Answers

Seems like there is no LIMIT/OFFSET in DQL anymore.

$qb = $em->createQueryBuilder();
//.. build your query
$q = $qb->getQuery();
$q->setFirstResult($offset);
$q->setMaxResults($limit);
$result = $q->getResult(); 
like image 61
DrColossos Avatar answered Oct 14 '22 19:10

DrColossos


I would Like to Contribute to this post and want to tell people that If you want to use DBAL with limit in your Unit Tests you can use following:

$client = static::createClient()
$em = $client->getContainer()->get('doctrine')->getManager();
$query = $em->createQuery('WRITE YOUR QUERY HERE');
$query->setFirstResult(0);
$query->setMaxResults(1);
$data = $query->getResult();

Same code can be used in controller also with some modifications :)

like image 29
Ashish Awasthi Avatar answered Oct 14 '22 17:10

Ashish Awasthi