Is it possible to use aggregate values in Doctrine_RawSql query? Here's what I'm trying to do:
$q = new Doctrine_RawSql();
$q->select('{q.*}, AVG(a.value) AS avg');
$q->from('-- complex from clause');
$q->addComponent('q', 'Question');
However, SQL created by Doctrine leaves only columns from table question
and omits aggregate value avg
.
I've never used Doctrine_RawSql before, but I have done raw SQL queries through Doctrine using either of these two methods:
and
It seems like these two methods would leave your original SQL intact.
I should note that I'm using Doctrine 1.2 within the context of Symfony 1.4 applications, but AFAIK, this would work for you regardless of what other frameworks you may be using.
Have you looked at the doctrine cookbook section on aggregates? They use
createQuery
method on the entity manager rather thanRawSql
objects.I'm no expert with doctrine, but this could be a good place to start!
Try putting the aggregate field in the SQL and then fetch it using curly brackets. I'm using SQL subquery.
It works for me.