Better way of Initializing from CFEclipse Bean Wizard
I have been using the CFEclipse to create the Bean, DAO, and Gateway CFCs. Not because it is a perfect tool, but while I am still learning how to manage working in a Framework better (and faster), the CFEclipse provides a nice foundation to start from.
Also, in the Lynda.com tutorials, David Gassner uses these wizards and customizes get/set bean cfc to be able to reinitialize the bean variables from a form. This is great and has helped me wrap my head around the easily confusing method of setter and getters. BUT! When the form doesn't have a variable that wants to be set by the bean, I have to pad cfif statements everywhere.... Not good and with large tables a real PITA. So I came up with a better way of doing it using StructKeyArrays....
Here is an example of a simple event table Bean created by CFEclipse:
<cfcomponent alias="blahblah.cfc.events">
<cfproperty name="eventid" type="numeric" default="0">
<cfproperty name="event" type="string" default="">
<cfproperty name="eventdesc" type="string" default="">
<cfproperty name="eventtime" type="string" default="">
<cfproperty name="displayme" type="string" default="">
<cfscript>
//Initialize the CFC with the default properties values.
this.eventid = 0;
this.event = "";
this.eventdesc = "";
this.eventtime = "";
this.displayme = "";
</cfscript>
<cffunction name="init" output="false" returntype="events">
<cfreturn this>
</cffunction>
<!--- getters and setters not shown here --->
David customizes the code a bit to accept a form structure and reset the values:
<cfcomponent alias="blahblah.cfc.events">
<!--- skipped over component cfproperty --->
<cffunction name="init" output="false" returntype="events">
<cfargument name="stValues" required="no" type="struct">
<cfif isdefined('arguments.stValues')>
<cfset this.eventid = arguments.stValues.eventid >
<cfset this.event = arguments.stValues.event >
<cfset this.eventdesc = eventdesc >
<cfset this.eventtime = arguments.stValues.eventtime >
<cfset this.displayme = arguments.stValues.displayme >
</cfif>
<cfreturn this>
</cffunction>
<!---setters and getters not shown again --->
So to fix it, instead of manually picking through and adding cfif isdefined's for each variable that may not be there, I used the form structures "FIELDNAMES" to pick out the columns that exist and assign them.
<cfcomponent alias="blahblah.cfc.events">
<cfproperty name="eventid" type="numeric" default="0">
<cfproperty name="event" type="string" default="">
<cfproperty name="eventdesc" type="string" default="">
<cfproperty name="eventtime" type="string" default="">
<cfproperty name="displayme" type="string" default="">
<cfscript>
//Initialize the CFC with the default properties values.
this.eventid = 0;
this.event = "";
this.eventdesc = "";
this.eventtime = "";
this.displayme = "";
</cfscript>
<cffunction name="init" output="false" returntype="events">
<cfargument name="stValues" required="no" type="struct">
<cfset var fieldarray = arraynew(1)> <!--- to hold form structure fieldnames --->
<cfif isdefined('arguments.stValues')>
<!--- form has fields --->
<cfif arraylen(structkeyarray(arguments.stValues) ) gt 0>
<cfset fieldarray = structkeyarray(arguments.stValues)>
<cfloop from='1' to='#arraylen(fieldarray)#' index='i'>
<!--- skip the "submit" button and the "fieldnames" column--->
<cfif findnocase('fieldnames',fieldarray[i]) eq 0 and findnocase('submit',fieldarray[i]) eq 0>
<cfset "this.#fieldarray[i]#" = evaluate(fieldarray[i]) >
</cfif>
</cfloop>
</cfif>
</cfif>
<cfreturn this>
</cffunction>
<!--- setters and getters not shown (duh) ---->
The results is a concise package that eliminates the need to re-evaluate each form field for existance.

There are no comments for this entry.
[Add Comment]