Reputation: 506
With doctrine and the query builder I write a SELECT MAX()
query, and when this result is passed to another query builder, as a parameter, the query works just fine.
But for some reason, I use native SQL query elsewhere in another Repository, and when I use $repo->getMaxMyThing()
, it return me an array of array like
array(array('1' => 42))
.
Not that if I want the result, I need to type: $max[0]['1']
(The 0 is a simple array index, but the '1' is a string associated key).
Did any method exists in the Doctrine Bundle to 'convert' it to a simple integer automatically?
Upvotes: 0
Views: 2112
Reputation: 10483
When you don't want Doctrine to return entities, you can use the get*ScalarResult() query methods.
Query#getScalarResult()
: Retrieves a flat/rectangular result set of scalar values that can contain duplicate data. The pure/mixed distinction does not apply.Query#getSingleScalarResult()
: Retrieves a single scalar value from the result returned by the dbms. If the result contains more than a single scalar value, an exception is thrown. The pure/mixed distinction does not apply.
Here is an example from the documentation:
$query = $em->createQuery('SELECT COUNT(u.id) FROM Entities\User u');
$count = $query->getSingleScalarResult();
$count
will be a single value (not an array) as requested.
Upvotes: 1