Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QueryBuilder alias for expr() in select() method

Tags:

doctrine-orm

is there any option to set alias for $qb->expr()->avg('e.value') in select() method of Doctrine2 query builder?

I have e.g. this query:

$qb->select($qb->expr()->avg('e.value'), 'e someAlias')->from('Entity', 'e');

But the average value is indexed by integer in the result, like this:

array(
  0 => array(
   'someAlias' => Entity {},
   1 => 2.5255,
  ),
);

Is it possible to change key of the average value to the defined string value?

like image 573
Pavel Plzák Avatar asked Jan 03 '13 20:01

Pavel Plzák


1 Answers

Try the following to set the alias:

$qb->select(array(
                 $qb->expr()->select()->avg('e.value').' AS aveAlias')
                ,'e someAlias')
           )->from('Entity', 'e');
like image 64
Lighthart Avatar answered Nov 28 '22 23:11

Lighthart