2011-12-18 29 views
40

Ho provato a scrivere una query (con sottoquery) ma non so come impostare un limite nella sottoquery. La mia domanda:Imposta LIMIT con doctrine 2?

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

Ma -> setMaxResults (5) non funziona. Nessun 'LIMIT' nella query SQL. Possiamo fare LIMIT semplice con la dottrina 2?

+0

MySQL non supporta ancora 'LIMIT e IN/ALL/ANY/SOME subquery' –

risposta

3

Il tuo setMaxResults (limite $) deve essere impostato sull'oggetto.

ad es.

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

Non funziona nel mio caso. Quando uso $ query_ids-> getDql() manca il LIMIT. – bux

+4

Perché questo ha upvotes? è esattamente lo stesso dell'OP ma è solo aggiungendo più assegnazioni alla stessa variabile. –

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

QUI nella seconda query deve essere superato il risultato della prima query ..

$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(); 
+1

La tua risposta funziona, è quello che uso nel mio codice di produzione da quando faccio la domanda. Ma nella mia domanda cerco di impostare un LIMIT sulla mia prima query, per usare il suo DQL in seconda query. (e fare solo una query del database). – bux

+0

È quel codice valido o lo pseudo codice? Quando provo bug di Symfony con 'Notice: Array to string conversion' posso provare un Hydrator personalizzato per restituire un array monodimensionale e imploderlo perché non sembra esserci uno già costruito che lo faccia: http: // docs.doctrine-project.org/it/2.1/reference/dql-doctrine-query-language.html#hydration-modes – HMR

+0

Fai attenzione se usi JOIN nel tuo DQL. Non funzionerà come previsto: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#first-and-max-result-items -dql-query-only –

0
$limit=5; // for exemple 

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

->setMaxResults($limit); 

$results = $query->getResult(); 

// Done 
+2

Si prega di inserire non solo il codice di lavoro, ma anche una breve spiegazione e un riassunto del proprio lavoro. –

+1

SORRY è la mia prima volta qui a fare un post, quindi per impostare un limite dobbiamo fare questa funzione nella query setMaxResults ($ limit) e non dimenticare di definire $ limit –

+0

@ Badrèddine - puoi modificare la tua risposta per migliorarla . SO ci consente di correggere i nostri errori quindi per favore approfittane. – APC

14

Io uso Doctrine\ORM\Tools\Pagination\Paginator per questo, e funziona perfettamente (dottrina 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); 
+0

La domanda è come limitare le entità delle subquery, ad es. entità correlate. –

1
$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]; 
+1

Benvenuti in StackOverflow, leggere questa guida per capire come scrivere una buona risposta: https://stackoverflow.com/help/how-to-answer È necessario aggiungere un contesto al codice e spiegare come/perché funziona. – Graham