I am trying to get Doctrine2 Entities, ordered by their ID which apparently is a String even though it contains only Numbers. So what I would like to do is something like this:
SELECT entity1, cast (entity1.id AS integer) AS orderId
FROM Namespace\Bla\MyEntity
ORDER BY orderId
Is there a way to do something like this in Doctrine2? Or, what would be the best practise to get my Result if i can't change the type of the id (due to customer requirements of course)?
Attention: I am not asking SQL Code, i am asking for a Doctrine2 Solution, preferably in DQL
Not sure if this works, but to access an entity ID you need the IDENTITY() DQL function. Try this:
I just did something similar in my own code yesterday. I was able to do:
I had to actually cast mine as money and then int, but if you don't have a decimal, you should not have that issue. You can also try casting as numeric or using convert instead of cast, but cast is better in this situation.
Why do you need entity1 as a column if you already have the same value in OrderID?
I think you want order by
entity1
. if your entity1 data type is integer then no need to change it into integer or if it is not then you should do it. below is query for you.try this one.Based on Jasper N. Brouwer answer, this is a little bit enhanced solution:
Now it should by possible to write DQL like this:
Try this one by with out changing the data type
You should be able to add your own function to implement this feature.
The class would look something like this:
You'll need to register your function:
Then you can use it:
PS: By adding the
HIDDEN
keyword, the aliasorderId
won't be in the results (and is only used for ordering).