Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch values likes PDO::FETCH_COLUMN in Doctrine?

I have the following code:

$repository = $this->entitymanager->getRepository('Intlist\Entity\Location');
$query = $repository->createQueryBuilder('l')
    ->select('l.country')
    ->groupBy('l.country')
    ->where('l.country != :empty_country')
    ->setParameter('empty_country', "")
    ->getQuery();

And I would like to obtain something like [ 'EN', 'FR', 'US' ], like PDO::FETCH_COLUMN would return. I tried getScalarResult() but it returns the same result as getArrayResult():

array (size=2)
  0 => 
    array (size=1)
      'country' => string 'DE' (length=2)
  1 => 
    array (size=1)
      'country' => string 'MM' (length=2)

I tried to use execute() as I have seen on some examples but it returns the same result as getArrayResult() and not a PDO statement.

Any idea?

like image 455
olvlvl Avatar asked Oct 20 '25 04:10

olvlvl


1 Answers

Hello this possibility was added in Doctrine ORM 2.10+ you can now do (following your example) :

$repository = $this->entitymanager->getRepository('Intlist\Entity\Location');
$query = $repository->createQueryBuilder('l')
    ->select('l.country')
    ->groupBy('l.country')
    ->where('l.country != :empty_country')
    ->setParameter('empty_country', "")
    ->getQuery()
    ->getSingleColumnResult();

References :

  • Documentation : https://www.doctrine-project.org/projects/doctrine-orm/en/2.10/reference/dql-doctrine-query-language.html#scalar-column-hydration
  • Merge request : https://github.com/doctrine/orm/pull/8919/files
like image 158
Sylvain Combes Avatar answered Oct 22 '25 07:10

Sylvain Combes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!