Home > ColdFusion > Learning OO in ColdFusion Pt. 3

Learning OO in ColdFusion Pt. 3

In my previous post I took a look at selecting an existing event, retrieving its details and populating an edit form with those details, then updating the event with the new values.  Today we will look at a very similar operation–adding a new event.  The process is quite similar.  First we instantiate an Event bean.  Now we can display our form and enter the details of our new event.  We submit the form, passing the form values to each corresponding Event bean property.  After that we just call our create() method in the EventDAO which we persisted in the Application scope.  That’s pretty much all there is to it.  At the top of my display file, I add some logic to check if the form has been submitted.  If it has, call the setters and pass in the corresponding form values.  The code looks like this.

<cfset objEventBean = createObject("component","eventRegistration2.components.barrett.beans.Event").init()/>
<cfif structKeyExists(form, "submit")>
<cfset objEventBean.setEventName(form.eventName)/>
<cfset objEventBean.setEventDescription(form.eventDescription)/>
<cfset objEventBean.setContactName(form.contactName)/>
<cfset objEventBean.setContactPhone(form.contactPhone)/>
<cfset Application.EventDAO.create(objEventBean)/>
<cflocation url="event_select.cfm" addtoken="false" />

Now let’s see if we can eliminate the duplicate code that exists between the editEvent form and the addEvent form.  Both pages begin by instantiating the Event bean.  The difference is that for edit, we have passed an eventID which we then use to retrieve an existing record.  In the case of a new event, no eventID is passed, rather the Event bean has the default value of 0.  How about we use Event.getEventID() to check the value?  If it’s not 0, we know it’s an existing record and we can then use EventDAO.retrieveByID() to grab the details and populate form.  That takes care of the retrieval and display.  We can now use one display page to handle creating a new event as well as editing an existing one.

On submit, we now need to know if we should call EventDAO.create() or EventDAO.update().  Let’s create an exists() method to check to see if  we have a new or existing record.  The exists() method will accept an Event bean as an argument. Just like we did above, the exists() method will check the eventID value.  If the value not 0 we return true.  Otherwise, we return false.  All this method does is answer the question, “Does this eventID exist already?”  That’s it.  Of course now we need to create our save() method.  The save() method looks like this:

<cffunction name="save" access="public" output="false" hint="I handle saving an Event" return="string">
<cfargument name="Event" type="eventRegistration2.components.barrett.beans.Event" required="true" hint="I am the Event bean"/>
<cfset var success = ""/>
<cfif exists(arguments.Event)>
<cfset success = update(arguments.Event)/>
<cfset success = create(arguments.Event)/>
<cfreturn success/>

The save() method is a sort of traffic cop.  It passes the Event bean argument to the exists() method and then asks what the method returned?  If true, the event exists and then calls the update() method.  Otherwise, the event is new and it will call the create() method.

One last thing.  Since we are no longer directly calling the create() or update() methods, we could change their access attribute to private.  I’ve been reading Object Oriented Thought Process and it is recommended that we expose only as much of our objects as is absolutely necessary.  So it makes sense to change these to private as they will be called by the exists() method now.

Categories: ColdFusion Tags:
  1. March 5, 2012 at 3:56 pm

    Well written post. I’m enjoying your series on Object Oriented programming with ColdFusion.

  2. Shane Montgomery
    June 10, 2012 at 4:07 pm

    Hi Ken, just an FYI, you can leave off the explicit call to the init() method of objects if you’re using “new” keyword to create your object via cfscript.

    E.G, objEventBean = new eventRegistration2.components.barrett.beans.Event();

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: