View comments | RSS feed

ColdFusion component example

A number of code examples in ColdFusion MX Developer's Guide reuse code, particularly queries. To illustrate the advantages of CFCs, these examples invoke the appropriate method in the CFC that appears in the following example. Although Macromedia recommends using CFCs to create structured, reusable code, some code examples in this manual contain queries within a CFML page, rather than invoking a CFC, in order to clearly illustrate a particular element of ColdFusion.

<cfcomponent>
   <cffunction name="allemployees" access="public" output="false"
      returntype="query">
      <cfset var getNames="">
      <cfquery name="getNames" datasource="cfdocexamples">
         SELECT * FROM Employee
      </cfquery>
   </cffunction>
   
   <cffunction name="namesalarycontract" access="public" output="false"
      returntype="query">
      <cfset var EmpList="">
      <cfquery name="EmpList" datasource="cfdocexamples">
         SELECT Firstname, Lastname, Salary, Contract
         FROM Employee
      </cfquery>
   </cffunction>
   
   <cffunction name="fullname" access="public" output="false"
      returntype="query">
      <cfset var engquery="">
      <cfquery name="engquery" datasource="cfdocexamples">
         SELECT FirstName + ' ' + LastName AS FullName
         FROM Employee
      </cfquery>
   </cffunction>
   
   <cffunction name="bydept" access="public" output="false" returntype="query">
      <cfset var deptquery="">
      <cfquery name="deptquery" datasource="cfdocexamples">
         SELECT Dept_ID, FirstName + ' ' + LastName
         AS FullName
         FROM Employee
         ORDER BY Dept_ID
      </cfquery>
   </cffunction>

   <cffunction name="employeebyURLID" access="public" output="false"
      returntype="query">
      <cfset var GetRecordtoUpdate="">
      <cfquery name="GetRecordtoUpdate" datasource="cfdocexamples">
         SELECT * FROM Employee
         WHERE Emp_ID = #URL.Emp_ID#
      </cfquery>
   </cffunction>
   
   <cffunction name="deleteemployee" access="public" output="false"
      returntype="void">
      <cfset var DeleteEmployee="">
      <cfquery name="DeleteEmployee" datasource="cfdocexamples">
         DELETE FROM Employee
         WHERE Emp_ID = #Form.Emp_ID#
      </cfquery>
   </cffunction>
   
   <cffunction name="distinctlocs"  access="public" output="false"
      returntype="void">
      <cfset var GetDepartments="">
      <cfquery name="GetDepartments" datasource="cfdocexamples">
         SELECT DISTINCT Location
         FROM Departmt
      </cfquery>
   </cffunction>
</cfcomponent>

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | KnowledgeBase | Bug Reporting

Version 7

Comments


CF_Doctor said on Aug 2, 2006 at 6:24 AM :
Not one of the above examples will work without the <cfreturn varName> before the closing </cffunction> tag.

Lame.
jpesolutions said on Aug 26, 2006 at 12:56 PM :
Actually the last two methods have a returntype="void" thus both of these methods do not return data and thus will work as coded in the example. You should make sure your comments are accurate before you being throwing insults.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00001068.htm