2015-03-24 111 views
2

我使用查詢生成器得到了一個查詢,並將其分配給$qb變量。它從PHP和數據庫都可以正常工作。現在,我嘗試使用查詢作爲子查詢象下面這樣:Symfony2原則查詢生成器作爲FROM子句中的子查詢

$subQuery = $qb->getQuery()->getSql(); 
    $query = 'select res.some_name 
       from ('.$subQuery.') as res'; 

,但我得到以下異常:

Caused by Doctrine\DBAL\Driver\PDOException: SQLSTATE[HY000]: General error: 1 no such column: res.some_name 

正如主義已經轉換$qb到這樣的地方主義轉換的原始SQL查詢。例如,有被稱爲legalentity_name的東西,但它顯示AS名1:

select res.some_name from (SELECT o0_.id AS id0, o0_.name AS name1, b1_.id AS id2, b1_.display AS display3, m2_.id AS id4, m2_.total AS total5 FROM Invoice i3_ INNER JOIN CodeableItem c4_ ON i3_.id = c4_.id INNER JOIN MonetaryItem m2_ ON i3_.id = m2_.id AND (1=1) INNER JOIN LineItem l5_ ON c4_.id = l5_.codeableItem_id LEFT JOIN MonetaryItem m6_ ON l5_.id = m6_.id AND (1=1) LEFT JOIN PresetLineItem p7_ ON c4_.id = p7_.codeableItem_id LEFT JOIN MonetaryItem m8_ ON p7_.id = m8_.id AND (1=1) INNER JOIN OrgUnit o0_ ON c4_.legalentity_id = o0_.id AND (1=1) INNER JOIN monetaryitem_listitem m9_ ON m2_.id = m9_.monetaryitem_id INNER JOIN BWListItem b1_ ON b1_.id = m9_.bwlistitem_id AND (1=1) INNER JOIN BWList b10_ ON b1_.bwlist_id = b10_.id AND (1=1) WHERE b10_.type = 'Vendor' GROUP BY c4_.legalentity_id, b1_.active, b1_.attributes, b1_.display, b1_.created, b1_.updated, b1_.lft, b1_.lvl, b1_.rgt, b1_.root, b1_.id, b1_.orgunit_id, b1_.bwlist_id, b1_.parent_id, b1_.rootou_id, m2_.created, m2_.updated, m2_.subtotal, m2_.total, m2_.description, m2_.id, c4_.number, c4_.externalId, c4_.status, c4_.overriddenDuringApproval, i3_.invoiceDate, i3_.dueDate, i3_.poNumber, m2_.rootou_id, c4_.image_id, c4_.legalentity_id, c4_.creator, c4_.owner_id ORDER BY o0_.name ASC, b1_.display ASC) as res 

我的問題是:我怎麼能使用來自$subQuery原始SQL?任何幫助將是非常有益的。乾杯!

回答

3

首先,DQL中的子查詢是不可能的。請參閱Selecting from subquery in DQL

其次,您將計算SQL從Doctrine查詢語言(DQL)放入子查詢中。這不起作用,因爲數據庫無法找到列,這是由於DQL在字段中爲字符/數字值添加了前綴。

這是實體可以在使用DQL時正確映射。

您將需要構建不使用DQL語言的子查詢(停止使用該查詢構建器,不確定是否有構建原始SQL的子查詢)。

相關問題