Solved

Code Snippets for Coldfusion Components

Posted on 2006-11-14
3
225 Views
Last Modified: 2013-12-24
Can someone provide me with some Code Snippets for a Coldfusion Component that will allow me to add, edit, update, & delete data.  

I would like code for the CFC File as well as the CFM file that I am using,  You don't have to custom name anything just use examples

like Datasource1, Variable2, Form3, etc..

 I just need to use this to store on Dreamweaver  

I will raise the amount of points if someone would like to throw in a custom tag snippet, and provide a significant value within the Component, or CFM file

Also, if the hints on the CFC can be very detailed with its hints I would appreciate it!!!!

0
Comment
Question by:rudodoo
3 Comments
 
LVL 3

Expert Comment

by:bhinshawnc1
ID: 17943511
0
 
LVL 20

Accepted Solution

by:
trailblazzyr55 earned 250 total points
ID: 17947461
here is an example of a CFC, now there's tons of different ways to go about doing something, this just shows some syntax through the update, delete, add, and get.... again, depending on your needs it could be very different... the methods in this cfc check for errors in the queries as well... the links provided above should give you an idea on how to use the cfc to do what you want... also I'd really recommend if you have Ben Forta's / Ray Camden's "Coldfusion Web Application Construction Kit" book, you read through the chapters discussing CFC's.....

http://www.amazon.com/Macromedia-ColdFusion-Web-Application-Construction/dp/0321223675/sr=1-1/qid=1163603218/ref=pd_bbs_sr_1/102-1153260-6204136?ie=UTF8&s=books

<cfcomponent hint="I start a CFC, I must be at the start of every CFC file, I contain methods to add, update, delete, and get records of customers.">

<cffunction name="addCustomer" access="public" returntype="struct" output="false" hint="I add a customer to the database">
      <cfargument name="custFirstName" required="yes" type="string">
      <cfargument name="custLastName" required="yes" type="string">
      <!--- define any variables used locally here in the "var" scope first --->
      <cfset var currDateTime = now()>
      <!--- the query status here is to provide feedback to your app on the success or failure of your query --->
      <cfset var qryStatus = structnew()>
      <cfset qryStatus['success'] = 0 >
      <cfset qryStatus['errMessage'] = "">
      <cfset qryStatus['errDetail'] = "">
      <cfset qryStatus['errCode'] = "">
      <cfset qryStatus['errType'] = "">
      <cftry>
            <cfquery datasource="YOUR_DSN">
                  INSERT INTO yourTable (firstName,LastName,createdDate)
                  VALUES ('#arguments.custFirstName#','#arguments.custLastName#','#currDateTime#')
            </cfquery>
            <cfcatch type="any">
                  <!--- provide some feedback for any errors --->
                  <cfset qryStatus['errMessage'] = cfcatch.Message>
                  <cfset qryStatus['errDetail'] = cfcatch.Detail>
                  <cfset qryStatus['errCode'] = cfcatch.ErrNumber>
                  <cfset qryStatus['errType'] = cfcatch.Type>
                  <cfset qryStatus['success'] = -1 >
            </cfcatch>
      </cftry>
      <cfreturn qryStatus>
</cffunction>

<cffunction name="updateCustomer" access="public" returntype="struct" output="false" hint="I update a customer in the database">
      <cfargument name="custFirstName" required="yes" type="string">
      <cfargument name="custLastName" required="yes" type="string">
      <cfargument name="custID" required="yes" type="numeric">
      <!--- define any variables used locally here in the "var" scope first --->
      <cfset var currDateTime = now()>
      <!--- the query status here is to provide feedback to your app on the success or failure of your query --->
      <cfset var qryStatus = structnew()>
      <cfset qryStatus['success'] = 0 >
      <cfset qryStatus['errMessage'] = "">
      <cfset qryStatus['errDetail'] = "">
      <cfset qryStatus['errCode'] = "">
      <cfset qryStatus['errType'] = "">
      <cftry>
            <cfquery datasource="YOUR_DSN">
                  UPDATE yourTable
                  SET firstName = '#arguments.custFirstName#',
                      LastName = '#arguments.custLastName#',
                        modifiedDate = '#currDateTime#'
                  WHERE customerID = #arguments.custID#
            </cfquery>
            <cfcatch type="any">
                  <!--- provide some feedback for any errors --->
                  <cfset qryStatus['errMessage'] = cfcatch.Message>
                  <cfset qryStatus['errDetail'] = cfcatch.Detail>
                  <cfset qryStatus['errCode'] = cfcatch.ErrNumber>
                  <cfset qryStatus['errType'] = cfcatch.Type>
                  <cfset qryStatus['success'] = -1 >
            </cfcatch>
      </cftry>
      <cfreturn qryStatus>
</cffunction>

<cffunction name="deleteCustomer" access="public" returntype="struct" output="false" hint="I delete a customer in the database">
      <cfargument name="custID" required="yes" type="numeric">
      <!--- define any variables used locally here in the "var" scope first --->
      <cfset var qryStatus = structnew()>
      <!--- the query status here is to provide feedback to your app on the success or failure of your query --->
      <cfset qryStatus['success'] = 0 >
      <cfset qryStatus['errMessage'] = "">
      <cfset qryStatus['errDetail'] = "">
      <cfset qryStatus['errCode'] = "">
      <cfset qryStatus['errType'] = "">
      <cftry>
            <cfquery datasource="YOUR_DSN">
                  DELETE FROM yourTable
                  WHERE customerID = #arguments.custID#
            </cfquery>
            <cfcatch type="any">
                  <!--- provide some feedback for any errors --->
                  <cfset qryStatus['errMessage'] = cfcatch.Message>
                  <cfset qryStatus['errDetail'] = cfcatch.Detail>
                  <cfset qryStatus['errCode'] = cfcatch.ErrNumber>
                  <cfset qryStatus['errType'] = cfcatch.Type>
                  <cfset qryStatus['success'] = -1 >
            </cfcatch>
      </cftry>
      <cfreturn qryStatus>
</cffunction>

<cffunction name="getCustomer" access="public" returntype="struct" output="false" hint="I get a customer in the database">
      <cfargument name="custID" required="yes" type="numeric">
      <!--- define any variables used locally here in the "var" scope first --->
      <cfset var myQry = "">
      <cfset var qryStatus = structnew()>
      <cfset var custInfo = structnew()>
      <cfset var rtnStruct = structnew()>
      <!--- the query status here is to provide feedback to your app on the success or failure of your query --->
      <cfset qryStatus['success'] = 0 >
      <cfset qryStatus['errMessage'] = "">
      <cfset qryStatus['errDetail'] = "">
      <cfset qryStatus['errCode'] = "">
      <cfset qryStatus['errType'] = "">
      <!--- the cust info here is to provide the first an last name of the customer based on the id passed to the method --->
      <cfset custInfo['firstName'] = "">
      <cfset custInfo['lastName'] = "">
      <cftry>
            <cfquery name="myQry" datasource="YOUR_DSN">
                  SELECT firstName, lastName
                  FROM yourTable
                  WHERE customerID = #arguments.custID#
            </cfquery>
            <cfcatch type="any">
                  <!--- provide some feedback for any errors --->
                  <cfset qryStatus['errMessage'] = cfcatch.Message>
                  <cfset qryStatus['errDetail'] = cfcatch.Detail>
                  <cfset qryStatus['errCode'] = cfcatch.ErrNumber>
                  <cfset qryStatus['errType'] = cfcatch.Type>
                  <cfset qryStatus['success'] = -1 >
            </cfcatch>
      </cftry>
      <cfif myQry.recordcount>
            <cfset custInfo['firstName'] = myQry.firstName>
            <cfset custInfo['lastName'] = myQry.lastName>
      </cfif>
      <!--- put both structs into one struct to return to your app --->
      <cfset rtnStruct['qryStatus'] = qryStatus >
      <cfset rtnStruct['custInfo'] = custInfo >
      <cfreturn rtnStruct>
</cffunction>
<!--- close out your component, this should conclude your .cfc file --->
</cfcomponent>
0
 

Author Comment

by:rudodoo
ID: 17997341
Thanks for the snippet, I appreciate it
0

Featured Post

Network it in WD Red

There's an industry-leading WD Red drive for every compatible NAS system to help fulfill your data storage needs. With drives up to 8TB, WD Red offers a wide array of solutions for customers looking to build the biggest, best-performing NAS storage solution.  

Join & Write a Comment

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now