Doctrine_RawSql查询的聚合值以用作教义对象

时间:2012-04-18 05:56:47

标签: mysql symfony1 doctrine aggregate-functions

我正在Symfony 1.4 / Doctrine 1.2 ORM上构建应用程序,并且在我的视图中实现MYSQL查询以用作教条对象时遇到问题

这是我需要融入教条对象的工作MYSQL查询:

SELECT u.id, u.username, p.picture AS picture, p.fb_user_id AS fb_user_id, p.fb_access_token AS fb_access_token, hot_scores.hot_user_score, SUM(b.incremental_points) as total_points
FROM sf_guard_user u
LEFT JOIN sf_guard_user_profile p ON p.user_id = u.id
LEFT JOIN mb_stats_bosspoints b ON b.user_id = u.id AND b.date >= '2012-03-20' AND b.date <= '2012-03-30' AND b.parent_genre_id = 10
LEFT JOIN (SELECT SUM(g.score) AS hot_user_score, g.user_id AS user_id
  FROM mb_score_by_genre g
  WHERE g.parent_genre_id = 10
  GROUP BY g.user_id
  ) hot_scores ON hot_scores.user_id = u.id
GROUP BY u.id
HAVING total_points >= 0
ORDER BY total_points DESC
LIMIT 10;

由于最后左连接中复杂的SELECT子查询,我无法弄清楚如何使用DQL为此创建查询。所以我决定使用doctrine raw sql。但我在另一篇文章[Here] [1]中发现,其他人在doctrine raw sql查询中使用聚合值时遇到了问题。无论如何我决定尝试一下。

这是我在一个返回查询或执行查询的函数中的doctrine原始sql查询:

$q = new Doctrine_RawSql();
$q->select('{u.id}, {u.username}, {p.picture} AS {picture}, {p.fb_user_id} AS {fb_user_id}, {p.fb_access_token} AS {fb_access_token}, {hot_scores.hot_user_score}, {SUM(b.incremental_points)} AS {total_points}');
$q->from("sf_guard_user u
LEFT JOIN sf_guard_user_profile p ON p.user_id = u.id
LEFT JOIN mb_stats_bosspoints b ON b.user_id = u.id AND b.date >= '".$start_date."' AND b.date <= '".$end_date."' AND b.parent_genre_id = ".$parent_genre_id."
LEFT JOIN (SELECT SUM(g.score) AS hot_user_score, g.user_id AS user_id
  FROM mb_score_by_genre g
  WHERE g.parent_genre_id = ".$parent_genre_id."
  GROUP BY g.user_id
  ) hot_scores ON hot_scores.user_id = u.id
GROUP BY u.id
HAVING total_points >= 0
ORDER BY total_points DESC
LIMIT ".$max);

$q->addComponent('u', 'sfGuardUser u');
$q->addComponent('p', 'sfGuardUserProfile p');
$q->addComponent('b', 'mbStatsBosspoints b');
$q->addComponent('g', 'mbScoreByGenre g');

if ($execute)
{
   return $q->execute();
}
else // just return query for pager to setQuery
{
   return $q;
}

然而,当我运行时,我收到以下错误:

SQLSTATE [42S22]:未找到列:1054'order子句'中的未知列'total_points'。查询失败:“

SELECT COUNT(*) as num_results FROM (SELECT DISTINCT u.id FROM sf_guard_user u
LEFT JOIN sf_guard_user_profile p ON p.user_id = u.id
LEFT JOIN mb_stats_bosspoints b ON b.user_id = u.id AND b.date >= '2012-04-12' AND b.date <= '2012-04-18' AND b.parent_genre_id = 10
LEFT JOIN (SELECT SUM(g.score) AS hot_user_score, g.user_id AS user_id
FROM mb_score_by_genre g
WHERE g.parent_genre_id = 10
GROUP BY g.user_id
) hot_scores ON hot_scores.user_id = u.id
GROUP BY u.id
HAVING total_points >= 0
ORDER BY total_points DESC
LIMIT 10) as results"`

有没有办法在使用doctrine raw sql查询时解决这个问题?

我尝试使用以下方法执行原始mysql代码的建议替代方法:

$doctrine = Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh();
$q = $doctrine->query(".......");
if ($execute)
{
   return $q->execute();
}
else // just return query for pager to setQuery
{
   return $q;
}

但是我得到以下错误,因为我不知道如何使用该方法只返回一个doctrine查询对象:

  

致命错误:调用未定义的方法PDOStatement :: offset()

我不知道如何只创建一个查询,以便我可以执行或返回查询。

所以这两个问题是:1。我可以修改我的doctrine raw sql调用以使用聚合函数吗? 2.如果没有,我怎样才能创建一个使用纯mysql的教义查询,并且仍会保留教条对象?

我迷失了,并希望朝着正确的方向前进。非常感谢。

0 个答案:

没有答案