Search code examples
symfonydoctrinedql

How to order on a calculated value without selecting it


I want to order entities returned from a Doctrine query on a calculated scalar value. Something like this in simplified DQL:

SELECT a, SOMEFUNC(a.foo,a.bar) AS b 
FROM MyStuff:Thing a 
ORDER BY b

The ordering works, but I don't actually want the scalar value b, it's just for sorting; I only want Thing entities back.

However with this query each result returned would be an array with the root object I want at [0] and then the redundant scalar value at [1]. This makes perfect sense, but it's not what I want and I don't know how to ask Doctrine to ignore the value.

How is this kind of ordering supposed to be done with Doctrine? How do I perform this ordering without selecting?


Solution

  • Use the AS HIDDEN clause:

    SELECT a, SOMEFUNC(a.foo,a.bar) AS HIDDEN b 
    FROM MyStuff:Thing a 
    ORDER BY b
    

    Source : https://speakerdeck.com/asm89/what-is-new-in-doctrine?slide=19