Another approach that I sometimes use is to declare an explicit argument to the
function call "inputArgs" of type "struct", and then replace all references to
FORM in the function to "arguments.inputArgs". This allows the main page can
the function with as "myCfc.myFunction(inputArgs = form)" and it works as
expected, but it forces that "main page" to play along and indicate,
explicitly, that the CFC is allowed to look at the form data.
(My biggest use of this is for CFCs that represent a specific form, so they can
render the form and also parse or process the results, with the "main page" not
caring about the form's contents. Modeling UI widgets this way has been handy.)
If at all possible I would go with Dave's suggestion of explicit arguments
because that is, well, more explicit, and it makes for a cleaner separation of
logic. Also, it prevents someone from submitting a hacked form that has more
fields in than you want, which could potentially be a security issue (depending
on your queries). Also, it allows you to define arguments with default values,
which could make your query functions more reusable.
Thanks
Mark
-----Original Message-----
From: Dave Watts [mailto:[email protected]]
Sent: Monday, March 02, 2009 2:51 PM
To: cf-talk
Subject: Re: cffunction in Component file
> I am planning to shift all the queries to one .cfc file. I am confused what
> if I have to pass the form values to the function in the .CFC files.
>
> <cffunction name="AddData" returntype="any"> <cfset var Qry_Data ="" >
> <cfquery name="Qry_Data" datasource="Platform" > INSERT INTO
> tblWebSiteBrand (FirstName,LastName,Phone) VALUES (<cfqueryparam
> cfsqltype="CF_SQL_VARCHAR" value='#form.FirstName#'>,
> <cfqueryparam cfsqltype="CF_SQL_VARCHAR"
> value='#form.LastName#'>,
> (<cfqueryparam cfsqltype="CF_SQL_INTEGER"
> value='#form.Phone#'>, </cfquery>
> <cfreturn Qry_Data>
> </cffunction>
That will actually work as-is. However, you're better off passing in parameters
rather than referencing external scopes from within your
functions:
<cffunction name="addData" ...>
<cfargument name="firstName" type="string" required="yes">
<cfargument name="lastName" type="string" required="yes">
<cfargument name="phone" type="string" required="yes">
...
</cffunction>
Then, when you invoke addData, you'd pass in the appropriate variables from
your form. The advantage of this approach is that your component is more
loosely coupled with the page that calls it. You might later want to pass data
from somewhere other than a form, and you could then do that without changing
your component.
Dave Watts, CTO, Fig Leaf Software
http://www.figleaf.com/
Fig Leaf Software provides the highest caliber vendor-authorized instruction at
our training centers in Washington DC, Atlanta, Chicago, Baltimore, Northern
Virginia, or on-site at your location.
Visit http://training.figleaf.com/ for more
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
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:320027
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4