【问题标题】:problems with calling CFC调用 CFC 的问题
【发布时间】:2011-08-15 02:42:36
【问题描述】:

在我的 CFM 中,我正在调用我的 CFC(cfc 被命名为客户,方法或函数被称为 ModifyCustomers),如下所示:

cfform method="post" action="?">
     <input type="hidden" action="ModifyCustomers" > 
...rest of form...

那么……

<!--- determine whether form was submitted and what action is --->

cfif isDefined("FORM") and isDefined("Action") and Action eq "ModifyCustomers">

    !--- create object for cfc ---

cfset AbcCFC = createObject("component", "customers") 


--- drop form data into method ---

cfset AbcCFC.ModifyCustomers(FORM)>


!--- the method will do stuff from form data--->


!---  result from the call to the method  ---

cfset wasBigSuccess = abcCFC.ModifyCustomers(FORM)

但是,我的 CFC 似乎没有被调用.....有什么建议吗?

更新

是的,我的 CFC 文件在同一目录中。

cfform method="post" action="?"

input type="hidden" action="ModifyCustomers"
...rest of form

/cfform (end of form)



!--- determine whether form was submitted and what action is ---

cfif isDefined("FORM") and isDefined("Action") and Action eq "ModifyCustomers"

    !--- create object for cfc ---

cfset AbcCFC = createObject("component", "customers") /


!--- drop form data into method ---

cfset AbcCFC.ModifyCustomers(FORM)

方法将运行...

检查结果... cfset wasBigSuccess = AbcCFC.ModifyCustomers(FORM)

/cfif

部分CFC代码:

cffunction name="ModifyCustomers" access="remote" returntype="String" output="false" 

        hint="Modify customers in the database"

   cfargument name="firstname" required="yes" type="string"

   cfargument name="lastname"  required="yes" type="string"

   cfargument name="email"     required="yes" type="string"

   cfargument name="manage"    required="yes" type="string"

  cfset var Customers = ""

  cfset var retVal = "0"

  cfset final = "0"

    <!--- If manage = Subscribe, run Addcustomers query. If record count is 0, user is already
        in the database, if record count is 1, query successflly ran and added user. Email is set as Unique
        in the database, so I used an "Ignore" below to bypass the system generated error and will show message
        stating that the user is already in database for newsletter--->

       <cfif Manage eq "Subscribe">
               <cfquery name="Addcustomers" datasource="LizDataSource" result="final">
     INSERT IGNORE INTO users (firstname, lastname, email)
          VALUES(
           <cfqueryparam value="#trim(form.firstname)#" cfsqltype="cf_sql_varchar">,
            <cfqueryparam value="#trim(form.lastname)#"  cfsqltype="cf_sql_varchar">,
            <cfqueryparam value="#trim(form.email)#"     cfsqltype="cf_sql_varchar">
                )       
               </cfquery>

              <cfif final.recordcount is "0">
                 <cfset retVal = "0">

                    <!---<cfoutput> Email #form.email# is already subscribed to the newsletter!
                    </cfoutput> --->

                    <cfreturn retVal>

我的表单数据没有写入我的数据库。在我更改调用 CFC 的方式之前,该查询正在运行。

【问题讨论】:

  • 你确定你的 createObject() 是正确的吗? Customers.cfc 是否与 cfm 文件位于同一目录中?如果不是,createObject() 的第二个参数应该是 CFC 的路径,以点表示法表示。例如createObject( 'component', 'path.to.Customers' )
  • 如果你能附上有用的代码。
  • 是什么让你相信它没有被调用?你有错误吗?如果是这样,什么错误?此外,您似乎在调用 ModifyCustomers() 两次。是这样吗?或者那只是示例代码?此外,ModifyCustomer 是否返回布尔值?正如 Dale 所说,这将有助于查看更多代码,包括 CFC 函数。
  • 我已经合并了您的两个未注册帐户,看起来您试图编辑一个答案,该答案应该是作为匿名用户对该问题的编辑。 使用与以前帐户相同的电子邮件创建帐户,并将 OpenID 与其关联。完成后,再次标记此帖子,我将进行最终帐户合并,让您完全控制此问题。最后,请查看编辑器中提供的格式化帮助并阅读FAQ

标签: coldfusion


【解决方案1】:

不要将表单范围传递给您的 CFC,而是尝试这样做:

<cfset AbcCFC.ModifyCustomers(argumentCollection = FORM) />

这会将表单结构的各个项目作为单独的参数传递,而不是简单地传递作为表单范围的整个结构。

此外,isDefined("form") 将在每个页面上返回 true。相反,你应该做点什么:

<cfif structKeyExists( form, "action" ) AND form.action EQ "ModifyCustomers" >

最后,你有一个带有“动作”属性的。没有这样的属性。我猜你的意思是:

<input type="hidden" name="action" value="ModifyCustomers" />

【讨论】:

    【解决方案2】:

    这是因为您没有将“管理”参数从您的 CFM 文件传递​​给 CFC?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多