4

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?

olvlvl
  • 2,396
  • 1
  • 22
  • 22

2 Answers2

3

You can extract values from your query result to get the array you want :

$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', "")
    ->getScalarResult();

$country_codes = array();
foreach($query as $result){
    $country_codes[] = $result['country'];
}

If you want native results, you can make a custom hydration mode, as explained in Doctrine doc.

Create a class extending AbstractHydrator:

namespace MyProject\Hydrators;

use Doctrine\ORM\Internal\Hydration\AbstractHydrator;

class ColumnHydrator extends AbstractHydrator
{
    protected function _hydrateAll()
    {
        return $this->_stmt->fetchAll(PDO::FETCH_COLUMN);
    }
}

Add the class to the ORM configuration:

$em->getConfiguration()->addCustomHydrationMode('ColumnHydrator', 'MyProject\Hydrators\ColumnHydrator');

Then use it:

$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();
$results = $query->getResult('ColumnHydrator');
Veve
  • 6,643
  • 5
  • 39
  • 58
  • Sorry, I should have said that I'm looking for a native function, like `PDO::fetchAll()` with `\PDO::FETCH_COLUMN`, I don't want to parse the result. – olvlvl Jan 07 '15 at 15:47
  • @olvlvl did you got the time to take a look at my edit? – Veve Jan 09 '15 at 08:16
  • Thanks @Veve, looks good. Too bad this hydrator is not packed with Doctrine. – olvlvl Jan 09 '15 at 11:26
3

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 :