对于派对而言,我正试图转向使用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>
Run Code Online (Sandbox Code Playgroud)
我打电话给.cfm页面;
<cfparam name="blogid" default="24" >
<cfinvoke component="td"
method="printBlogEntries"
searchString="#blogid#"
returnvariable="blogentries" >
<cfoutput>#blogentries#</cfoutput>
Run Code Online (Sandbox Code Playgroud)
返回的错误是;
The value returned from the listBlogEntries function is not of type query.
Run Code Online (Sandbox Code Playgroud)
如果我手动运行查询并执行cfdump,一切看起来都应该如此,所以我在cfc中显然做错了.然而,它现在的方式几乎是CFWACK书中给出的例子的复制品.
指针将非常受欢迎(正如任何推荐阅读该主题!)
在函数"listBlogEntries"中,您有一个名为"getBlogEntries"的查询.您收到错误,因为此函数此刻未返回任何内容.只需cfreturn在查询后添加一个.
此外,如果您使用的是ColdFusion 9或更高版本,则可以取消<cfset var getBlogEntries = "">使用函数局部变量范围"local".它做同样的事情.
<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>Run Code Online (Sandbox Code Playgroud)
而@Cory,他在他的组件中使用了两个函数,因为可能性是多个其他函数需要listBlogEntries()生成的查询.
| 归档时间: |
|
| 查看次数: |
5483 次 |
| 最近记录: |