I generally make my CFCs as generic as possible. You are updating a
product and looking in the form scope for everything. It'll work,
sure. But, what if in the future you get an entire cart. Then you
won't be looking in the form scope, you'll be looking at something in
the session or database. In that case, you'll want to be able to loop
over something and pass stuff into the function which would be very
tricky if the function is stuck in the form scope. Also, there's no
reason to copy the arguments over to variables. Variables would be used
more if you are actually instantiating a CFC as an object. I assume you
are not because if you did that, you'd probably pass an object into the
update product.
Here's a way I'd re-write your function to make it more abstract:
<cffunction name = "update_product" displayname = "update_product" hint
= "Update specified product" output = "false">
<cfargument name="product_id" type="string" required="yes">
<cfargument name="name" type="string" required="false" default="">
<cfargument name="description" type="string" required="false"
default="">
<cfargument name="regular_price" type="numeric" required="false"
default="">
<cfargument name="featured" type="string" required="false" default="">
<cfargument name="special" type="string" required="false" default="">
<cfargument name="special_price" type="numeric" required="false"
default="">
<cfargument name="photo_150" type="string" required="false" default="">
<cfargument name="photo_400" type="string" required="false" default="">
<cfquery name = "qUpdateProduct" datasource="#application.dsn#">
update products
set name = <cfqueryparam cfsqltype="cf_sql_varchar"
value="#arguments.name#">,
description = <cfqueryparam cfsqltype="cf_sql_varchar"
value="#arguments.description#">,
regular_price = <cfqueryparam cfsqltype="cf_sql_decimal"
value="#arguments.regular_price#" scale="2">,
featured = <cfqueryparam cfsqltype="cf_sql_char"
value="#arguments.featured#">,
special = <cfqueryparam cfsqltype="cf_sql_char"
value="#arguments.special#">,
special_price = <cfqueryparam cfsqltype="cf_sql_decimal"
value="#arguments.special_price#" scale="2">,
photo_150 = <cfqueryparam cfsqltype="cf_sql_varchar"
value="#arguments.photo_150#">,
photo_400 = <cfqueryparam cfsqltype="cf_sql_varchar"
value="#arguments.photo_400#">
where product_id = "#arguments.product_id#"
</cfquery>
</cffunction>
-Jake Churchill
Rick Faircloth wrote:
> Hi, all... still trying to get a handle on using CFC's.
>
> Here's an update method I wrote and I'd like to know
> what can be changed to make it better. Such as,
> why not use the form scope in the query? And, why
> not just use the application scope for the dsn, instead
> of converting it to the variables scope?
>
> If components have access to the url, form, session, and
> application scopes, etc., why not use them in the component?
> What's the benefit of converting them?
>
> <cffunction name = "update_product"
> displayname = "update_product"
> hint = "Update specified product"
> output = "false">
>
> <cfargument name="product_id" type="string" required="yes">
> <cfset variables.product_id = "#arguments.product_id#">
>
> <cfquery name = "qUpdateProduct" datasource="#application.dsn#">
>
> update products
>
> set name = <cfqueryparam
> cfsqltype = "cf_sql_varchar" value = "#form.name#">,
> description = <cfqueryparam
> cfsqltype = "cf_sql_varchar" value = "#form.description#">,
> regular_price = <cfqueryparam
> cfsqltype = "cf_sql_decimal" value = "#form.regular_price#" scale
> = "2">,
> featured =
> <cfqueryparam cfsqltype = "cf_sql_char" value =
> "#form.featured#">,
> special =
> <cfqueryparam cfsqltype = "cf_sql_char" value =
> "#form.special#">,
> special_price = <cfqueryparam
> cfsqltype = "cf_sql_decimal" value = "#form.special_price#" scale
> = "2">,
> photo_150 = <cfqueryparam
> cfsqltype = "cf_sql_varchar" value = "#form.photo_150#">,
> photo_400 = <cfqueryparam
> cfsqltype = "cf_sql_varchar" value = "#form.photo_400#">
>
> where product_id =
> "#variables.product_id#"
>
> </cfquery>
>
>
> </cffunction>
>
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to
date
Get the Free Trial
http://ad.doubleclick.net/clk;207172674;29440083;f
Archive:
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:314220
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4