2012-12-22 24 views
13

Nella mia classe repository io uso il seguente codice per interrogare:Doctrine 2: risultato Query come array associativo

$query = $this->getEntityManager()->createQuery(" 
    SELECT s.term, COUNT(s.term) AS freq 
    FROM App\Entities\SearchTerm s 
    GROUP BY s.term 
    ORDER BY s.term ASC 
"); 

$result = $query->getResult(); 

Il risultato che ottengo è qualcosa di simile:

array (size=4) 
    0 => 
    array (size=2) 
     'term' => string '' (length=0) 
     'freq' => string '1' (length=1) 
    1 => 
    array (size=2) 
     'term' => string 'foo' (length=3) 
     'freq' => string '1' (length=1) 
    2 => 
    array (size=2) 
     'term' => string 'bar' (length=3) 
     'freq' => string '2' (length=1) 
    3 => 
    array (size=2) 
     'term' => string 'baz' (length=3) 
     'freq' => string '2' (length=1) 

Ma avrei preferito un array associativo come risultato:

array (size=4) 
    '' => string '1' (length=1) 
    'foo' => string '1' (length=1) 
    'bar' => string '2' (length=1) 
    'baz' => string '2' (length=1) 

questo è possibile senza un extra per-loop per costruire l'array desiderato?

risposta

2

In realtà da qualche parte la trasposizione deve essere eseguita. Vedere 14.7.4. Hydration Modes su cosa viene restituito da ->getResult() e quali modalità/varianti alternative esistono già.

È anche possibile aggiungere la propria modalità di idratazione in un punto centrale. Questo è spiegato in 14.7.4.5. Custom Hydration Modes.

13

So che il suo vecchio ma oggi ho dovuto fare quasi lo stesso, la mia soluzione senza un idratante personalizzato

  • INDICE PER s.term
  • modificare il getResult() per essere sicuri

come

$result = $query->getQuery()->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY); 

o

$result = $query->getQuery()->getResult(2); 
  • formato il risultato

come

$resultNeeded = array_map(function($value) { return $value['freq']; }, $result); 
+12

pleeeease non utilizzare 'getResult (2)' - come buona pratica OOP quando disponibile utilizzare sempre una costante per il futuro leggibilità – jmaculate

7

Se si desidera una matrice è possibile utilizzare the getArrayResult method.

Ottiene l'array di risultati per la query.

Alias ​​per l'esecuzione (null, HYDRATE_ARRAY).

$result = $query->getQuery()->getArrayResult(); 

Quindi, utilizzando questo metodo vi darà the exact same result as using the constant HYDRATE_ARRAY.

+0

questo non sarà restituito il desiderato formato. – Roman

+0

@Roman Perché no? – Wilt

+0

Dimostrare che ho torto, ma penso che non restituirà l'array associativo desiderato. L'ho provato e restituirà ogni riga di risultato come un proprio elemento di matrice che contiene all'interno dell'array associativo. – Roman

1

Dopo molte ricerche ho trovato alcune soluzioni per oggetto doctrine per la conversione di matrice. Quando abbiamo associato oggetti nel risultato.

1.

$person = $em->find('Person', 2); 
$da = array(); 
     $person = (array) $person; 
     foreach($person as $i=>$d) { 
      if (is_object($d)) { 
       $d = (array) $d; 
       foreach($d as $si=>$sd){ 
        if (is_object($sd)) { 
         //var_dump('after convert array'); 
         $da[$i][$si] = (array) $sd ; 
         //var_dump($da[$i][$si]); 
        } else { 
         $da[$i][$si] = $sd ; 
         //var_dump($da[$i][$si]); 
        } 
       } 

      } else { 
       $da[$i] = $d; 
       //var_dump($da[$i]); 
      } 
     } 

echo '<pre>'; print_r($da); echo '<pre>'; exit; 

2.

$query = $em->createQuery('SELECT w FROM Person w WHERE w.Id = 2'); 
$person = $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY); 
echo '<pre>'; \Doctrine\Common\Util\Debug::dump($person); exit; 

3.

$result = $em->createQueryBuilder(); 
      $person = $result->select('p') 
      ->from('PsnPersonPsn', 'p') 
      ->where('p.Id= 1') 
      ->getQuery() 
      ->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY); 
      echo '<pre>'; \Doctrine\Common\Util\Debug::dump($person); exit;