【问题标题】:How do I write an ORM statement to return filtered data from a data object?如何编写 ORM 语句以从数据对象返回过滤后的数据?
【发布时间】:2013-01-10 15:34:38
【问题描述】:

好的。我刚刚开始在我的 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 查询返回的正是我所需要的:

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

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

【问题讨论】:

    标签: orm coldfusion hql


    【解决方案1】:

    您正在运行的 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 });
    

    【讨论】:

    • 非常感谢。这完美地工作。一旦我们(我和我的同事)看到您的回答,就像“Doh ...”。这很有意义。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2017-10-07
    • 2011-05-13
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 2017-02-11
    相关资源
    最近更新 更多