Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Code Snippets for Coldfusion Components

Posted on 2006-11-14
3
Medium Priority
?
268 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 20

Accepted Solution

by:
trailblazzyr55 earned 750 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…
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…
Suggested Courses

927 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