Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set LIMIT with doctrine 2?

Tags:

doctrine-orm

I trying to write a query (with subquery) but i don't know how set a limit in my subquery. My query:

$query_ids = $this->getEntityManager()       ->createQuery(         "SELECT e_.id         FROM MuzichCoreBundle:Element e_         WHERE [...]         GROUP BY e_.id")      ->setMaxResults(5)     ;  $query_select = "SELECT e       FROM MuzichCoreBundle:Element e        WHERE e.id IN (".$query_ids->getDql().")       ORDER BY e.created DESC, e.name DESC"     ;  $query = $this->getEntityManager()       ->createQuery($query_select)       ->setParameters($params)     ; 

But ->setMaxResults(5) doesn't work. No 'LIMIT' in the SQL query. Can we do simple LIMIT with doctrine 2 ?

like image 327
bux Avatar asked Dec 18 '11 19:12

bux


2 Answers

$query_ids = $this->getEntityManager()       ->createQuery(         "SELECT e_.id         FROM MuzichCoreBundle:Element e_         WHERE [...]         GROUP BY e_.id")      ->setMaxResults(5)      ->setMaxResults($limit)      ; 

HERE in the second query the result of the first query should be passed ..

$query_select = "SELECT e       FROM MuzichCoreBundle:Element e        WHERE e.id IN (".$query_ids->getResult().")       ORDER BY e.created DESC, e.name DESC"     ;   $query = $this->getEntityManager()       ->createQuery($query_select)       ->setParameters($params)       ->setMaxResults($limit);     ;  $resultCollection = $query->getResult(); 
like image 90
Darshita Avatar answered Sep 20 '22 00:09

Darshita


I use Doctrine\ORM\Tools\Pagination\Paginator for this, and it works perfectly (doctrine 2.2).

$dql = "SELECT p, c FROM BlogPost p JOIN p.comments c"; $query = $entityManager->createQuery($dql)                        ->setFirstResult(0)                        ->setMaxResults(10);  $paginator = new Paginator($query, $fetchJoinCollection = true); 
like image 33
Oskar Avatar answered Sep 17 '22 00:09

Oskar