1

I'm using Symfony2 and Doctrine to get an array of values from the database. I want a list of values from one field of the database table.

I'm using the following code:

        return $this->createQueryBuilder('ad')
        ->select('ad.extId')
        ->where('ad.extId is not NULL')
        ->getQuery()
        ->getArrayResult();

It returns the following array:

Array
(
    [0] => Array
        (
            [extId] => 3038
        )
)

I would like to revieve the following:

Array
(
    [0] => 3038
)

Any one an idee to get this direct from Doctrine without an extra foreach loop?

Tom
  • 1,547
  • 7
  • 27
  • 50
  • 1
    this would be it http://stackoverflow.com/questions/11657835/how-to-get-a-one-dimensional-scalar-array-as-a-doctrine-dql-query-result/11685062#11685062 or this http://stackoverflow.com/questions/11657835/how-to-get-a-one-dimensional-scalar-array-as-a-doctrine-dql-query-result/11685062#11685062 – Confidence Apr 17 '17 at 19:15

1 Answers1

2

Without custom hydrator, I don't think it is possible out of the box. However, you could do this (similar to foreach):

$arr = $this->createQueryBuilder('ad')
        ->select('ad.extId')
        ->where('ad.extId is not NULL')
        ->getQuery()
        ->getArrayResult();

return array_map(function($a){ return $a['extId']; }, $arr);
Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85