使用doctrine2加入symfony2中的子查询

时间:2015-04-09 10:33:35

标签: php sql join doctrine-orm subquery

我正在尝试在symfony2应用程序中执行以下SQL。

SQL的摘要是

Select the latest currency conversion rate for each branch.
The currencies should be active.
The branches should be verified.

原始SQL

SELECT b.id, r1.* FROM rate as r1 JOIN 
 ( SELECT branch, from_currency, to_currency, max ( time ) as latest_time FROM `rate` group by branch, from_currency, to_currency ) as r2 
 ON r1.branch = r2.branch 
    and r1.from_currency = r2.from_currency 
    and r1.to_currency = r2.to_currency 
    and r1.time = r2.latest_time
 join branch b 
    on b.id = r1.branch and b.verified_by is not null
 join currency c1
    on r1.from_currency = c1.id 
 join currency c2
    on r1.to_currency = c2.id
 where c1.active = 1 and c2.active = 1

首先,我尝试使用querybuilder,在这种情况下似乎不支持。 现在我意识到我需要使用NativeQuery。 但我对填充ResultSetMapping很困惑。 我跟着http://doctrine-orm.readthedocs.org/en/latest/reference/native-sql.html但是不能说清楚。

问题是:

  • 有人可以帮我填一下这里的resultsetmapping吗?
  • 或者更好的方法来获取结果集?

P.S。如果我还要添加模式,请告诉我。我只是为了简洁而跳过它。

0 个答案:

没有答案