您可以使用Native SQL和地圖查詢結果的實體:
use Doctrine\ORM\Query\ResultSetMapping;
$rsm = new ResultSetMapping;
$rsm->addEntityResult('AppBundle:Charges', 'charges')
->addEntityResult('AppBundle:Refunds', 'refunds')
->addFieldResult('charges', 'id', 'id')
->addFieldResult('charges', 'currency', 'currency')
->addFieldResult('charges', 'total_transactions', 'total_transactions')
->addFieldResult('charges', 'total_volume', 'total_volume')
->addFieldResult('charges', 'commission', 'commission')
->addFieldResult('refunds', 'total_payouts', 'total_payouts')
;
$sql = "
SELECT
charges.id,
charges.currency,
charges.total_transactions,
charges.total_volume,
charges.commission,
refunds.total_payouts
FROM
(SELECT ...very long query...) charges
LEFT JOIN
(SELECT ...very long query...) refunds ON charges.id = refunds.id AND charges.currency = refunds.currency
WHERE some_field = ?
";
$query = $this->getEntityManager()->createNativeQuery($sql, $rsm);
$query->setParameter(1, $name);
$entities = $query->getResult();
您可以使用[查詢生成器](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference /查詢BUI lder.html)來創建這個查詢。 – thhan
@thhan,我找不到任何有關我的情況的信息。僅用於表(實體)的SELECT,而不是來自兩個連接的SELECT。如果你知道如何做到這一點,你可以提供一個例子嗎? –
你可以使用兩個先前的queryBuilder作爲你的'from'的源碼 – Veve