Column Hydrator #5002

Closed
opened 2026-01-22 14:56:20 +01:00 by admin · 4 comments
Owner

Originally created by @inssein on GitHub (Feb 4, 2016).

Originally assigned to: @Ocramius on GitHub.

I quickly wanted to get a column's value from the database, and realized there was no good way of returning the data.

Array Hydrator:

$em->createQueryBuilder()->select('e.column')->from('Entity', 'e')->getQuery()->getArrayResult()

Output: [['prop' => 'x'], ['prop' => 'y']]

The desired output in this given scenario for me, would be ['x', 'y'], and that would be super easy with a Column Hydrator ($this->_stmt->fetchAll(PDO::FETCH_COLUMN);).

Should I submit this as a PR? I am not sure if there has been a previous discussion around this subject, but I didn't find any from a quick glance.

Originally created by @inssein on GitHub (Feb 4, 2016). Originally assigned to: @Ocramius on GitHub. I quickly wanted to get a column's value from the database, and realized there was no good way of returning the data. Array Hydrator: ``` $em->createQueryBuilder()->select('e.column')->from('Entity', 'e')->getQuery()->getArrayResult() ``` Output: `[['prop' => 'x'], ['prop' => 'y']]` The desired output in this given scenario for me, would be `['x', 'y']`, and that would be super easy with a Column Hydrator (`$this->_stmt->fetchAll(PDO::FETCH_COLUMN);`). Should I submit this as a PR? I am not sure if there has been a previous discussion around this subject, but I didn't find any from a quick glance.
admin added the ImprovementWon't Fix labels 2026-01-22 14:56:20 +01:00
admin closed this issue 2026-01-22 14:56:21 +01:00
Author
Owner

@Ocramius commented on GitHub (Feb 4, 2016):

@inssein simply:

return array_map(
    function (array $row) { return $row['y']; },
    $em->createQuery('SELECT e.column FROM Entity e')->getArrayResult()
); 

array_column also does this ;-)

A new hydrator for this sort of functionality is unnecessary.

@Ocramius commented on GitHub (Feb 4, 2016): @inssein simply: ``` php return array_map( function (array $row) { return $row['y']; }, $em->createQuery('SELECT e.column FROM Entity e')->getArrayResult() ); ``` [`array_column`](http://php.net/manual/en/function.array-column.php) also does this ;-) A new hydrator for this sort of functionality is unnecessary.
Author
Owner

@inssein commented on GitHub (Feb 4, 2016):

@Ocramius yeah, that's what I have been doing previously, but I think I have done it enough times that it made sense to do it as a Hydrator. Just thought I'd contribute it back, but no worries.

@inssein commented on GitHub (Feb 4, 2016): @Ocramius yeah, that's what I have been doing previously, but I think I have done it enough times that it made sense to do it as a Hydrator. Just thought I'd contribute it back, but no worries.
Author
Owner

@Ocramius commented on GitHub (Feb 4, 2016):

@inssein calling array_column or configuring a custom hydrator is the same amount of effort: I wouldn't add a new hydrator for something this trivial, as it just increases the amount of code in the library for no strong reason ;-)

@Ocramius commented on GitHub (Feb 4, 2016): @inssein calling `array_column` or configuring a custom hydrator is the same amount of effort: I wouldn't add a new hydrator for something this trivial, as it just increases the amount of code in the library for no strong reason ;-)
Author
Owner

@inssein commented on GitHub (Feb 4, 2016):

sounds good.

@inssein commented on GitHub (Feb 4, 2016): sounds good.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5002