Mar 6, 2008

Easy CFC

This is a simple coldfusion pages and the accompanying cfc.  
There are two functions that do different things. It might be
clear as mud so don't hesitate to drop me a line if you get lost,
but here is the idea, I am craeting two functions and invoking
them in differnt ways one example is through createObject and
the other is the cfinvoke tag.

(first our index.cfm page)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>
Untitled Document
</title>
</head>
<body>

<!---
Using cfset to call an object and output a value
In a cfc I have a component that has a function called timer.
Below I an 'instanciating' the object and
assigning it to a variable named 'time'
instead of using cfinvoke I am using the function
createObject. createObject has several parameter settings.
For a CFC and if that CFC is in the same directory as this file.
Then I simply set the createObject to call a component named time.
--->

<cfset time = createObject('component', 'myCfc')>

<!---
Below I am outputting the variable I set above.
Notice I am using the dot notation.
time.timer()
Notice the paranthesis. If my object
took an argument I would place it there.
Like this time.timer(today,tommorow,yesterday,etc)
But I have nothing to pass since
this is only a simple example.
--->

<cfoutput>
#time.x("This is a argument/parameter being passed to my object")#
</cfoutput>
<br<br>

<!---Here is a quick example of a cfc that does some arithmetic.--->

<!--- We are using the invoke method (tag) to pull this object into play --->

<cfinvoke component="myCfc" method="z" returnvariable="goober">

<cfoutput>
#goober#
</cfoutput>

</body>
</html>

(my next file is called myCfc.cfc)

<cfcomponent hint="amigo formats the date...if argument gets passed then i append it to the amigo string">
<cffunction
name="x"
description="the function name is x">
<cfargument
name="today"
type="string"
required="no">

<cfset amigo = timeFormat(now(), "h:MM")>

<cfif isdefined("arguments.today")>
<cfset amigo = arguments.today & " " & amigo>
</cfif>
<cfreturn amigo>
</cffunction>

<cffunction name="z">
<cfset x = 100>
<cfset y = 5>
<cfreturn x/y>
</cffunction>
</cfcomponent>

Labels: , , , , , , , ,

Mar 3, 2008

CFC Simplicity

Ok so here is my quick and dirty primer on CFCs. CFCs are the defacto method for keeping your development nice and clean. For this example i am using a simple query and then invoking it in a web page. There are two files you need the CFC and the CFM calling the CFC.

So here are the files assuming you have a database registered in your Coldfusion administrator.

index.cfm
qryPerson.cfc

For the index page here are the contents with explanations and the cfc with explanations.

<!---First we are invoking the cfc component, it's method

which is the same name as the component, and giveing

the return variable the same name. You can get

creative but you will atleast have to have these

three items. --->

<cfinvoke component="qryPerson" method="qryPerson" returnvariable="qryPerson">

<!---Here ais the only argument I am passing the first name is frank.--->

<cfinvokeargument name="first" value="Frank">

<!---Closing the invoke.--->

</cfinvoke>

<!---Now I am dumping out the return query.--->

<cfdump var="#qryPerson#">

<!---If you want to actually present the return...do this...--->
<cfoutput query="qryPerson">

#first_name# #last_name# #userID#

</cfoutput>

Here is the qryPerson.cfc (component).

<!---This cfcomponent can be modified to alter combinations

of first and last or even first, last and userID.

But your if statement would be more complex.--->

<!---Keeping it very simple here first cones the component name.--->

<cfcomponent>

<!---The function comes next make sure it is sent to return type query--->

<cffunction name="qryPerson" access="public" returntype="query">

<!---Here is my arguement I am calling it is not requiried--->

<cfargument name="first" type="string" required="yno">

<!---Then my query it is very simple but notice the embedded

if statement. Notice that the variable says with

arguments.first ARGUMENTS is what you use inside the CFC unless

you set it to a different variable but for simplicity I pulled

it directly, which is perfectly acceptable to do.--->

<cfquery datasource="emp" name="qryPerson">

Select * from emp

<cfif isdefined("arguments.first")>

where first_name = '#arguments.first#'

</cfif>

</cfquery>

<!---Now we return the query to this cfreturn tag..

just use the name of the query.--->

<cfreturn qryPerson>

</cffunction>

</cfcomponent>

Any questions? drop me a line at frank_tudor@yahoo.com

Labels: , , , , , , ,