0

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?

4
  • 2
    show code of method getMaxMyThing Commented Jan 22, 2016 at 18:59
  • 1
    Have you tried to use getSingleScalarResult()? Commented Jan 23, 2016 at 21:36
  • @A.L It works, thank you and sorry I completely forget to give you a response. Answer the question so I can validate. Commented Feb 10, 2016 at 10:40
  • @rickozoe Thanks for the ping! I added an answer. Commented Feb 10, 2016 at 12:30

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.