it-swarm-pt.tech

Defina LIMIT com doctrine 2?

Estou tentando escrever uma consulta (com subconsulta), mas não sei como definir um limite na minha subconsulta. Minha consulta:

$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)
    ;

Mas -> setMaxResults (5) não funciona. Não há 'LIMIT' na consulta SQL. Podemos fazer um LIMIT simples com doctrine 2?

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

AQUI, na segunda consulta, o resultado da primeira consulta deve ser passado.

$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();
49
Darshita

Eu uso Doctrine\ORM\Tools\Pagination\Paginator para isso, e funciona perfeitamente (doutrina 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);
16
Oskar

Seu setMaxResults ($ limit) precisa ser definido no objeto.

por exemplo.

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

$query = $this->getDoctrine()->getEntityManager()->createQuery(
           '// your request')

->setMaxResults($limit);

 $results = $query->getResult();

// Done
0
Badrèddine
 $qb = $this->getDoctrine()->getManager()->createQueryBuilder();  
 $qb->select('p') ->from('Pandora\UserBundle\Entity\PhoneNumber', 'p');
$qb->where('p.number = :number');
$qb->OrWhere('p.validatedNumber=:number');
$qb->setMaxResults(1);
$qb->setParameter('number',$postParams['From'] );
$result = $qb->getQuery()->getResult();
 $data=$result[0];
0
ajay singh