<!---//

A guy on another discussion board asked this question:
" Hello,
I finally started to try to learn cfc's this morning. Can't get it to work and I don't understand the documentation.
Would someone give me a real basic example please. Please assume I know nothing.

For example on the .cfc page within component tag I want to query a user database
select whatever from users where userid = #userid#

(what is the cfc reccommended method for saying 'only do this query if isDefined userid'?)
then how do I grab the output of this when needed on the pages in the application?
"
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
So I responded with this code and decided it best that all our easyCFM.com members who want to get a grip on the
cfc rage can have a really simple gentle intro to CFCs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//--->

<!---********* START firstCFC.cfc *********--->

<cfcomponent>

<!---// inside a cfc you can create many functions to do many things //--->

<!---//
This function will retreive all info about a single user based on
the userid passeduser from the DB. Now you pass into this cfc 1
thing (the userID of the user you want info on.
//--->

<!---//
Now we use the <CFFUNCTION> tag to say "Hey I want a new function here and I want it to be named myFunction, access = public(so all
files can call this function and return a query to the page and variable that called me.
//--->

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

<!---//
Now we have to declare a argument, which is a way of receiving the userid that was passed to this function.
We say "name it myArgument and we are expecting a numeric value to be passed to us (the userID) and required=true
(meaning if there isn't a numeric value passed we aren't going to process your request
//--->

<cfargument name="myArgument" type="string" required="true">

<!---//
Now we set set up a varibale to use as our query name. We use the word var in front of the varName to make it
only available to this function, that way we don't read or write values from another variable with the same name.
Just set myVar to "" and we will be just fine.
//--->

<cfset var myVar="">

<!---//
Now using our new variable lets query up!!! Use <CFLOCK to prevent another instance of this from
causing "problems".
//--->

<CFLOCK name="firstCFC_Lock" throwontimeout="yes" timeout="10" type="exclusive">
    <CFQUERY name=
"myVar" datasource="#REQUEST.dsn#">
        SELECT *
        FROM Users
        WHERE UserID=#ARGUMENTS.myArgument# <!---// use ARGUMENTS scope in front of the name of your argument/userID //--->
    </CFQUERY>

</CFLOCK>

<!---//
Now use <CFRETURN> to return the set of info returned by the query.
To do that just return the name of the query or "myVar"
//--->
<cfreturn myVar>

</cffunction>
<!---// end the function //--->



</cfcomponent><!---// end the component //--->

<!---***************************** END firstCFC.cfc *********************************************************************--->

<!--- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Below is the page somePage.cfm that has a the code to call and use this cfc
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ->

<!--- somePage.cfm--->

<html>
<head><title>
somePage.cfm</title></head>
<body>
<!---Now create an instance of the cfc so we can use its function(s)--->

<!--- var=CreateObject("type","name of cfc being called")--->

<cfset mycfc=CreateObject("Component","firstCFC")>

<!---now lets use our newly created instance of the cfc and by usin the name of the
var and a "." between the var name and the name of the function we want to use
we can "pass info and receive it into the var we are going to set--->

<!---mycfc.myFunction(number in here is the userID we want info on)--->

<cfset varToHoldQueryResultsReturnedFromFunction=mycfc.myFunction(5)>

<!---Now we can use #varToHoldQueryResultsReturnedFromFunction# to output
query results--->

<!---we say gt 0 because if the query set is empty that means no user found--->
<cfif #varToHoldQueryResultsReturnedFromFunction# gt 0>
    <cfoutput query="varToHoldQueryResultsReturnedFromFunction">

        #UserID#<br>
        #FirstName#<br>
        #LastName#<br>
    </cfoutput>
</cfif>

About This Tutorial
Author: Michael "Newkirk" MacDonald
Skill Level: Beginner 
 
 
 
Platforms Tested: CFMX
Total Views: 95,901
Submission Date: January 23, 2005
Last Update Date: June 05, 2009
All Tutorials By This Autor: 5
Discuss This Tutorial
  • This tutorial sucks. Who names a variable "varToHoldQueryResultsReturnedFromFunction"

  • I know how to create cfc's and Im a newb. So even though I know how to pass varibles over, I still dont understand the public attribute and all that crap. The guy who wanted help said assume he knows nothing. Yet the tutorial goes on tell you how to create cfc step by step, but doesnt explain what each attribute means. This tutorial needs more detail in explaining the attributes. If your going to make a tutorial for beginners, beginners need to know the definitions! Sure I could make a component now, but I still dont know what the hell most the attributes do!

  • is the correct syntax....sorry...I left the RecordCount off

  • hm... if i change: to it works fine. Am I doing something wrong?

  • Complex object types cannot be converted to simple values. The expression has requested a variable or an intermediate expression result as a simple value, however, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values. The most likely cause of the error is that you are trying to use a complex value as a simple one. For example, you might be trying to use a query variable in a tag. This was possible in ColdFusion 2.0 but creates an error in later versions. The Error Occurred in C:\Inetpub\wwwroot\Aiden\test\somePage.cfm: line 26 24 : 25 : 26 : 27 : 28 : #UserID#

  • Make sure you save the file as .cfc and You can either put the CFC and ColdFusion pages in a separate folder, like an asset, or, in the same folder with the group of CFML pages that use the CFC. Regardless, Dreamweaver MX will locate them and make all CFCs in your defined site available to all other CFML pages in the website. You can store CFCs anywhere in your webroot, but it is a best practice to keep them in a separate folder.

  • Ok where do we save this file to as I get the message can not find blah blah blah

  • This comment block: should read as:

Advertisement


Website Designed and Developed by Pablo Varando.