0

How can you add a select based on a conditional to the doctrine query builder?

I'd like to replicate SQL similar to this:

select p.id, p.id = 3 as first_result
from problem p
order by first_result desc, p.id
Geoff Maddock
  • 1,700
  • 3
  • 26
  • 47

1 Answers1

0

As per docs you could use expression and mark this column as HIDDEN (not to include in results) but can be use in query like in your case to order results

DQL would look like

SELECT p, p.id = 3 AS HIDDEN first_result
FROM YourProblemEntity p 
ORDER BY first_result DESC, p.id

Or you can introduce CASE expression

SELECT p, 
CASE WHEN p.id = 3 THEN 1 ELSE 0 END AS HIDDEN first_result
FROM YourProblemEntity p 
ORDER BY first_result DESC, p.id

See DQL SELECT Examples

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118