Doctrine 2: Query result as associative array
Asked Answered
L

5

21

In my Repository class I use the following code to query:

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

The result I get is something like:

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)

But I would rather have an associative array as a result:

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

Is this possible without an extra for-loop to build the desired array?

Limann answered 22/12, 2012 at 12:37 Comment(1)
This trait give the result in expected format gist.github.com/ajaxray/9ef4c5dcc4e2aa881240370b6f27c8f4Booby
C
23

I know its old but today I had to do almost the same, my solution without a custom hydrator

  • INDEX BY s.term
  • modify the getResult() to be sure

like

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

as

$resultNeeded = array_map(function($value) { return $value['freq']; }, $result);
Congresswoman answered 22/7, 2013 at 8:55 Comment(0)
M
11

If you want an array you can use the getArrayResult method.

Gets the array of results for the query.

Alias for execute(null, HYDRATE_ARRAY).

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

So using this method will give you the exact same result as using the constant HYDRATE_ARRAY.

Metrics answered 2/12, 2015 at 18:19 Comment(1)
Prove me i am wrong, but i think it wont return the desired associative array. I tried it and it will return each result-line as an own array element which holds inside the associative array.Ist
B
5

Use INDEX BY :

$em = $this->getEntityManager();
$query = $em->createQuery('SELECT c FROM SomeBundle:Configuration c INDEX BY c.name');
$query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);

I saw it here and it works fine : https://coderwall.com/p/crz4dq/defining-a-column-to-be-the-key-of-the-result-hydrated-as-array-in-doctrine-2

Boysenberry answered 20/1, 2020 at 14:31 Comment(0)
C
2

Actually somewhere the transposition has to be done. See Hydration Modes about what is returned by ->getResult() and which alternatives modes/variants already exist.

You can also add your own hydration mode at a central place. That is explained in Custom Hydration Modes.

Carlotacarlotta answered 22/12, 2012 at 13:43 Comment(0)
F
1

After lot of search I have found some solutions for doctrine object to array conversion. When we have associated objects in result.

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;
France answered 16/1, 2018 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.