Coldfusion CFC - 函数返回的值不是查询类型?

时间:2014-03-13 00:27:42

标签: coldfusion

对于派对来说,我试图转向使用CFC以简化事情。在这个阶段,我只是试图找到我的脚并理解它们 - 使用CFWACK 9作为指导。

然而,我的第一次尝试让我难过!

这是我在CFC中所拥有的内容;

<cffunction name="listBlogEntries" returntype="query" output="false" 
      access="remote" hint="Find all blog entries by blogid, sorted by id desc">

    <cfargument name="blogid" required="true" default="24">
    <cfset var getBlogEntries = "">

    <cfquery name="getBlogEntries">
        SELECT  ID, entry_title 
        FROM    blog_entries 
        WHERE  blogID='#ARGUMENTS.blogid#'
        ORDER BY ID DESC 
        LIMIT 10
    </cfquery> 
</cffunction>

<cffunction name="printBlogEntries" returntype="void" access="remote" 
      hint="Lookup blog entries and return formatted">

        <cfargument name="blogid" required="true" default="24">
        <cfset var qBlogEntries  = listBlogEntries(ARGUMENTS.blogid)>
        <cfoutput query="qBlogEntries">
            <h1>Entry ID: #qBlogEntries.ID#</h1>
            <h3>Entry Title: #qBlogEntries.entry_title#</h3>
        </cfoutput>

        <cfreturn>

</cffunction>

我的电话.cfm页面;

<cfparam name="blogid" default="24" >

<cfinvoke component="td" 
      method="printBlogEntries" 
      searchString="#blogid#" 
      returnvariable="blogentries" >

<cfoutput>#blogentries#</cfoutput>

返回的错误是;

The value returned from the listBlogEntries function is not of type query. 

如果我手动运行查询并执行cfdump,一切看起来都应该如此,所以我在cfc中显然做错了。然而,它现在的方式几乎是CFWACK书中给出的例子的复制品。

非常感谢指针(正如任何推荐阅读的主题一样!)

1 个答案:

答案 0 :(得分:9)

在您的函数&#34; listBlogEntries&#34;中,您有一个名为&#34; getBlogEntries&#34;的查询。您正在收到错误,因为此功能目前尚未返回任何内容。只需在查询后添加cfreturn即可。

此外,如果您使用ColdFusion 9或更高版本,则可以取消使用<cfset var getBlogEntries = "">并使用函数局部变量范围&#34; local&#34;。它做了同样的事情。

<cffunction name="listBlogEntries" returntype="query" output="false"
       access="remote" hint="Find all blog entries by blogid, sorted by id desc">

    <cfargument name="blogid" required="true" default="24">
    <cfquery name="local.getBlogEntries">
            SELECT ID, entry_title 
            FROM   blog_entries 
            WHERE  blogID = <cfqueryparam value="#ARGUMENTS.blogid#"
                                    cfsqltype="cf_sql_integer">
            ORDER BY ID DESC 
            LIMIT 10
    </cfquery> 

    <cfreturn local.getBlogEntries>
</cffunction>

而@Cory,他在他的组件中使用了两个函数,因为赔率是多个其他函数需要listBlogEntries()生成的查询。