2013-10-16 58 views
1

我正在使用propel 1.6.7中的retreiving數據掙扎。從多個連接的查詢中獲取聚合列到單個表格

這是我想達到的查詢的示例:

select count(1) as amount, p1.local_id FROM panel_data pd 
     JOIN panel_data_has_code pp1 on (pd.panel_data_id = pp1.panel_data_id) 
     JOIN panel_code p1 on (pp1.panel_code_id = p1.panel_code_id AND p1.type='equipment' AND model_id = 'my_model_id') 
     JOIN panel_data_has_code pp2 on (pd.panel_data_id = pp2.panel_data_id) 
     JOIN panel_code p2 on (pp2.panel_code_id = p2.panel_code_id AND p2.type='model' AND p2.local_id='my_local_id') 
     GROUP BY p1.local_id 

但是每當我試圖構建的Propel ORM適當的標準,我有一個問題 - 顯然推動總是意味着加入別名表名,每當我嘗試使用add join條件或在select方法中使用別名時。我已經用簡單的filterBy方法替換了多個連接條件(我總是使用內部連接,所以效果將是相同的),但是我仍然遇到與retreiving分組列(p1.local_id)有關的問題。

這是我與ATM的工作代碼:

PanelDataQuery::create() 
      ->select(array('p1.localId')) 
      ->withColumn('count(1)', 'amount') 
      ->usePanelDataHasCodeQuery('pp1')->usePanelCodeQuery('p1')->groupByLocalId()->filterByType($type)->endUse()->endUse() 
      ->usePanelDataHasCodeQuery('pp2')->usePanelCodeQuery('p2')->filterByType('model')->filterByLocalId($model)->endUse()->endUse() 
      ->find(); 

上面的語句返回一個錯誤:

Unable to execute SELECT statement [SELECT count(1) AS amount, panel_code.LOCAL_ID AS \"p1.localId\" FROM panel_data INNER JOIN panel_data_has_code pp1 ON (panel_data.PANEL_DATA_ID=pp1.PANEL_DATA_ID) INNER JOIN panel_code p1 ON (pp1.PANEL_CODE_ID=p1.PANEL_CODE_ID) INNER JOIN panel_data_has_code pp2 ON (panel_data.PANEL_DATA_ID=pp2.PANEL_DATA_ID) INNER JOIN panel_code p2 ON (pp2.PANEL_CODE_ID=p2.PANEL_CODE_ID) WHERE p1.TYPE=:p1 AND p2.TYPE=:p2 AND p2.LOCAL_ID=:p3 GROUP BY p1.LOCAL_ID] [wrapped: SQLSTATE[42000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]The multi-part identifier \"panel_code.LOCAL_ID\" could not be bound.]

顯然,SELECT子句會導致問題。任何想法如何強制推動使用表別名,而不是將其翻譯爲表名?

非常感謝您的幫助。

回答

0

實測值部分解決方案:

PanelDataQuery::create() 
    ->withColumn('p1.local_id','localId') 
    ->withColumn('count(1)', 'amount') 
    ->select(array('localId','amount')) 
    [...] 

看起來工作得很好。 Altought我會很欣賞任何其他解決方案。如果在addJoinCondition中處理類似的問題,仍然會很棒。

當我更換:

->filterByType('model')->filterByLocalId($model) 

從原來的查詢,以:

->addJoinCondition('p2', 'p2.localId = ?', $model) 
->addJoinCondition('p2', "p2.type = 'model'"); 

我得到了類似的問題,因爲在錯誤中指出:推進轉變p2.localId到panel_code.local_id這ISN」 t正確(查詢中包含多個帶別名的panel_code表連接)。

相關問題