2013-01-10 20 views
3

好的。我剛剛開始在我的Coldfusion應用程序中使用ORM。到現在爲止它一直很好。我碰到了這個障礙。我有這兩個表:如何編寫ORM語句以從數據對象返回過濾的數據?

enter image description here

這裏是我使用的數據加載到頁面的代碼。 if的第二部分是默認加載,第一部分是用於將列表過濾到特定類別的部分。

<cfif form.filtercat neq ''> 
    <cfset load = ormexecuteQuery('from product_spec_cats as cats inner join cats.product_spec_cat_prod_cat_lnk as link WHERE link.spl_prod_cat_id = #form.filtercat#',{},false)> 
<cfelse> 
    <cfset load = entityload('product_spec_cats')> 
</cfif> 

cfelse查詢返回這這正是我需要的:

enter image description here

cfif查詢返回這是一個問題,因爲有兩個孩子每個父母陣列英寸

enter image description here

所以,我的問題是,我怎麼寫HQL在相同的結構默認查詢返回的數據,仍然能夠對數據進行篩選?

回答

7

您正在運行的HQL是同時選擇product_spec_catsproduct_spec_cat_prod_cat_link實體,因爲你沒有定義,你要選擇什麼:

from product_spec_cats as cats 
inner join cats.product_spec_cat_prod_cat_lnk as link 
WHERE link.spl_prod_cat_id = #form.filtercat# 

查詢是基本相同,在一個正常的SQL查詢select * from ...。你想要做的是這樣的:

select cats 
from product_spec_cats as cats 
inner join cats.product_spec_cat_prod_cat_lnk as link 
where link.spl_prod_cat_id = #form.filtercat# 

取決於你們的關係如何設置,你甚至可以不需要內部聯接,你可以寫你的查詢是這樣的:

from product_spec_cats as cats  
where cats.product_spec_cat_prod_cat_lnk.spl_prod_cat_id = #form.filtercat#` 

最後,順便說一句,我會建議你使用的查詢參數,尤其是你堅持從form範圍的東西進入查詢:

ormExecuteQuery(" 
    select cats 
    from product_spec_cats as cats 
    inner join cats.product_spec_cat_prod_cat_lnk as link 
    where link.spl_prod_cat_id = :catID 
", { catID = form.filtercat }); 
+0

比非常感謝你。這工作完美。一旦我們(我的同事和我)看到你的答案,就像是「Doh ...」。它非常有意義。再次感謝。 – Sollinger04

+0

高興地幫助:) –

相關問題