Solved

Code Snippets for Coldfusion Components

Posted on 2006-11-14
3
235 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
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…

808 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