我正在 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 来为此创建查询。所以我决定使用学说 raw sql。但是我在另一篇文章 [Here][1] 中发现,其他人在使用原始 sql 查询中的聚合值时遇到了问题。我还是决定试一试。
这是我在返回查询或执行查询的函数中的原则原始 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"`
有什么方法可以解决这个问题,同时仍然使用学说原始 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;
}
但是我收到以下错误,因为我不知道如何使用该方法来返回一个学说查询对象:
致命错误:调用未定义的方法 PDOStatement::offset()
我不知道如何只创建一个查询,以便我可以执行或返回查询。
所以这两个问题是: 1. 我可以修改我的学说原始 sql 调用以使用聚合函数吗?2. 如果没有,我怎样才能创建一个使用纯 mysql 的学说查询并且仍然会水合学说对象?
我迷路了,我希望能朝着正确的方向前进。非常感谢。