Built with
Model Glue 2.0

Visit the Coldbox 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"

Success:

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

A handler named addEmployeeAction was sitting in config/ModelGlue.xml, and it was waiting ... listening for your action (Submit click).

<event-handler name="addEmployeeAction">
      <broadcasts>
        <message name="processAddEmployee" />
      </broadcasts>
      <views>
        <include name="body" template="dspEmployeeSignup.cfm">
          <value name="xe.addEmployee" value="addEmployee" />
        </include>
      </views>
      <results>
        <result name="ValidationError" do="addEmployee" redirect="true" />
        <result do="view.template" />
      </results>
    </event-handler>
</code>

The broadcasts tag hollered to the listener at the top of the same file, HEY! I'm telling you to process this employee!

Here's the listener

<message-listener message="processAddEmployee" function="addEmployee" />

Model Glue automatically "cranked up" your cfc's for you, so it knows where the addEmployee method is.

So now, the function has been called in controller/controller.cfc. Here's what that looks like:

<cffunction name="addEmployee" access="public" returnType="void" output="false">
	  <cfargument name="event" type="any">      
      <cfscript>
	  var Employee = GetModelGlue().GetBean("Employee");
	  var emp_fname = arguments.event.getValue("emp_fname");
	  var emp_lname = arguments.event.getValue("emp_lname");
	  var emp_age = arguments.event.getValue("emp_age");
	  var successText = "Yes, this employee was added!";	  
	  var success = arguments.event.setValue("success", successText);
	  
	  if(not len(trim(emp_fname))){
      arguments.event.setValue("emp_fnameError", "Please enter a First Name.");
      arguments.event.addResult("ValidationError");
	  
	  }	  
	  
	 Employee.processAddEmployee(emp_fname, emp_lname, emp_age);	   
	 </cfscript>      
</cffunction>

The first line calls my Employee.cfc, which actually *ADDS* the employee

var Employee = GetModelGlue().GetBean("Employee");

The next three lines simply extract the values from each form field

I set some message variables for display, then some empty field checks

The last line uses my Employee object to insert the values into my database

Employee.processAddEmployee(emp_fname, emp_lname, emp_age);

Let's have a look at Employee.cfc

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

Looks prety straightforward. It accepts the arguments, grabs the dsn config setting, then inserts those puppies into my database

DONE!