For the form types I needed a one dimensional array. And as I think I will use it more often I added this to doctrine as a Hydrator.

The "Column"-Hydrator class:

namespace .... Doctrine\Hydrator;

use Doctrine\ORM\Internal\Hydration\AbstractHydrator, PDO;

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

(Used from http://stackoverflow.com/questions/11657835/how-to-get-a-one-dimensional-scalar-array-as-a-doctrine-dql-query-result/31316925#31316925)

Adding it to the configuration:

doctrine:
    dbal:
        default_connection:   default

        connections:
            default:
            ...

    orm:
        ...
        hydrators:
            COLUMN_HYDRATOR: Runalyze\Bundle\CoreBundle\Doctrine\Hydrator\ColumnHydrator
        ...

Use the new hydrator:

$queryBuilder->getQuery()->getResult("COLUMN_HYDRATOR");

Works fine for me using Symfony 3.1.x. I thought that would be possible with Doctrine without adding something like that, but it's always good to see that it's so simple!

Add a comment

Next Post Previous Post