Built with
Coldbox

Visit the Model Glue Version of this Site

Home

Complex Data Types

Lists

Arrays

Structures

Advanced Structures

CFC Functionality (It's a specialized complex data type w/methods)

Request a username using your email

Add Employee
How it works

Show Employees

* For sake of simplistic examples, <cfqueryparam> is not used in any of the examples. Please be sure to use <cfqueryparam> in your production code

Thanks to Jeff Peters for writing his great book, "ColdFusion Lists, Arrays, and Structures"

Your form submission was successful!

Here's what happened in sequential order when you click submit on the Add Employee form

A handler named Employee.cfc was sitting in handlers/Employee.cfc, and it was waiting ... listening for your action (Submit click).

<cffunction name="processAddEmployee" access="public" returntype="void" output="false">
   <cfargument name="Event" type="coldbox.system.beans.requestContext">
               
 <cfscript>
	var rc = event.getCollection();
    var dsn = getDatasource(').getName();
	var empService = createObject("component", "Model.employeeService").init(dsn);
	empService.addEmployee(rc.emp_fname, rc.emp_lname, rc.emp_age);
	setNextEvent("Employee.dspAddEmployee");				
 </cfscript> 
</cffunction> 

Notice this is WAY different from Model Glue. Coldbox is taking care of more stuff for us - such as broadcasts, listeners, etc. The code above initializes our EmployeeService Object which is the part that actually does the insert into our database. Whatever form values were submitted, imagine Coldbox scooped them up automatically into a collection we save in a variable named rc.

We now reference our form variables using the rc prefix. i.e. rc.emp_fname, etc. We pass these to our EmployeeService object to add with a cfquery.

<cffunction name="addEmployee" access="public" returnType="void" output="false">
     <cfargument name="emp_fname" type="string" />
     <cfargument name="emp_lname" type="string" />
     <cfargument name="emp_age" type="numeric" />  
        
      <cfquery datasource="#variables.dsn#">
      insert into tblemployees (emp_fname, emp_lname, emp_age)
      values ('#arguments["emp_fname"]#', '#arguments["emp_lname"]#', #arguments["emp_age"]#)      
      </cfquery>            
  </cffunction>
 

Now, we simply drop the user into a new event (a new view/page) with this line:

setNextEvent("Employee.dspAddEmployee");

DONE!