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


Question: Using conditionals to set tag parameters
Answer: You problem is you're trying to do a complex conditional within the tag parameters, that's not possible.

Here's a good article regarding parsing conditionals within tags:
http://johndwells.com/blog/expressionengine-parse-order-advanced-conditionals-as-tag-parameters

As you state, the problem is simple conditionals versus complex conditionals.

The problem with your second code:

{if {segment_2} == '' OR {exp:segment_search keyword="/^P\d+$/" segments="2" regex="yes"}}
    {exp:low_search:results
        {if segment_1 == "foo"}group_id="6"{/if}
        {if segment_1 == "bar"}group_id="7"{/if}
        limit="4"
        paginate="bottom"}
{if:else}
    {exp:low_search:results
        query="{segment_2}"
        {if segment_1 == "foo"}group_id="6"{/if}
        {if segment_1 == "bar"}group_id="7"{/if}
        limit="4"
        paginate="bottom"}
{/if}


...Is that you won't be able to have the opening tag in a separate conditional to the closing tag - EE parses conditionals at a separate stage to tags. It's not linear (working from top to bottom).

This may work:

{if {segment_2} == '' OR {exp:segment_search keyword="/^P\d+$/" segments="2" regex="yes"}}
    {exp:low_search:results
        {if segment_1 == "foo"}group_id="6"{/if}
        {if segment_1 == "bar"}group_id="7"{/if}
        limit="4"
        paginate="bottom"}

        {title}

    {/exp:low_search:results}
{if:else}
    {exp:low_search:results
        query="{segment_2}"
        {if segment_1 == "foo"}group_id="6"{/if}
        {if segment_1 == "bar"}group_id="7"{/if}
        limit="4"
        paginate="bottom"}

        {title}

    {/exp:low_search:results}
{/if}


...Where each tag pair is contained within the same conditional.

There's an additional problem in your revised conditional that is showing a parse error:

{if {segment_2} == '' OR {exp:segment_search keyword="/^P\d+$/" segments="2" regex="yes"}}


If there is no keyword, that tag will return an empty string, resulting in your conditional looking like this prior to EE parsing it: {if {segment_2} == '' OR }

...Which will result in the parse error.  This is safer:

{if {segment_2} == '' OR '{exp:segment_search keyword="/^P\d+$/" segments="2" regex="yes"}'}


With the quotes, it can be parsed correctly as {if {segment_2} == '' OR ''}

Or how I'd approach it, by including in an embed and doing all the conditionals in the parent:

{if {segment_2} == '' OR {exp:segment_search keyword="/^P\d+$/" segments="2" regex="yes"}}
    {embed=template-group/search-results group_id="{if segment_1 == "foo"}6{/if}{if segment_1 == "bar"}7{/if}" query=""}
{if:else}
    {embed=template-group/search-results group_id="{if segment_1 == "foo"}6{/if}{if segment_1 == "bar"}7{/if}" query='query="{segment_2}"'}
{/if}


And in the embed template:

    {exp:low_search:results
        group_id="{embed:group_id}"
        {embed:query}
        limit="4"
        paginate="bottom"}

        {title}

    {/exp:low_search:results}


Note: for the first embed, query will be an empty string which is fine to pass as a parameter.