Doctrine: Order by alias with the same name as the column

I have the following DQL (Doctrine1.2):

$dql->select('sum(t.column1) column1, t.column2') ->from('Table t') ->groupBy('t.column2') ->orderBy('column1'); 

But the generated SQL is the same as for the following DQL:

 $dql->select('sum(t.column1) column1, t.column2') ->from('Table t') ->groupBy('t.column2') ->orderBy('t.column1'); //Ordered by column1 not by sum(t.column1) 

Is it possible to force Doctrine to use the alias column1 instead of the column name t.column1 ?

I need this because I would like to have the same output format from the model for two very similar DQLs. (the same template has been displayed recently). Another DQL is as follows:

 $dql->select('t.column1, t.column2') ->from('Table t') ->orderBy('t.column1'); 

A possible workaround is to select sum(t.column1) twice. First with the alias column1 and the second time with some other alias, why after passwd to the orderBy function, but this does not seem to be the clearest solution.

Any suggestions?

thanks

+4
source share
1 answer

Could you try this approach? Just put "sum (t.column1)" in order, instead of trying to use an alias:

 $dql->select('sum(t.column1) as column1, t.column2') ->from('Table t') ->groupBy('t.column2') ->orderBy('sum(t.column1)'); 
+5
source

Source: https://habr.com/ru/post/1335232/


All Articles