3

好的。我刚刚开始在我的 Coldfusion 应用程序中使用 ORM。到目前为止,它一直进展顺利。我碰到了这个障碍。我有这两张表:

在此处输入图像描述

这是我用来将数据加载到页面中的代码。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正是我需要的:

在此处输入图像描述

查询返回 this,这cfif是一个问题,因为每个父数组中有两个子数组。

在此处输入图像描述

所以,我的问题是,如何编写 HQL 以返回与默认查询相同结构的数据,并且仍然能够过滤数据?

4

1 回答 1

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#

select * from ...该查询与普通 SQL 查询中的 a基本相同。你想要做的是:

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 });
于 2013-01-10T19:21:28.973 回答