Aggregate values in Doctrine_RawSql queries

╄→尐↘猪︶ㄣ 提交于 2019-12-05 19:27:21

I've never used Doctrine_RawSql before, but I have done raw SQL queries through Doctrine using either of these two methods:

Doctrine_Manager::getInstance()->getCurrentConnection()->fetchAssoc("YOUR SQL QUERY HERE");

and

$doctrine = Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh();
$result = $doctrine->query('YOUR SQL QUERY HERE');

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 than RawSql 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.

$q = new Doctrine_RawSql();
$q->select('{s.*}, {s.avg} AS avg');
$q->from('(SELECT q.*, AVG(value) AS avg FROM Question AS q) AS s');
$q->addComponent('s', 'Question');

It works for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!