Using the event handling features in ORM in ColdFusion 9 makes it easy to automatically add audit information such as last updated and last modified by to all objects, well technically tables, in the database. I'm about to launch my first ColdFusion 9 application and the technique below has worked well and sped up development.
Step 1: Decide on audit colum names
There really is no right or wrong way here. Go for something readable and easily understandable (for those lucky future developers who will look at your code!). Once you pick them add them as properties to your CFC:
Step 2: Turn on Event Handling and Hander
In your Application.cfc in addition to ormEnabled=true, turn on event handling and an event handler like so:
this.ormsettings = { eventHandling=true, eventHandler="myEventHandler" };
The eventHandler points to a valid CFC "myEventHandler.cfc" which is what we will create next.
Step 3: Create an Event Handler CFC (See I told ya!)
Step 4: Add code to preInsert and preUpdate functions
The preInsert and preUpdate functions in the event handler CFC are the last functions that get run before ColdFusion hands over to Hibernate for persistence (a fancy way of saying saving to database). All of the functions in eventHandler take in the entity as an argument called entity (if you are new to cfscript within the parenthesis its saying a type of any and an argument called entity). We simply call the various functions and pass in the values. I found I got an error at first matching the session userID to createBy and lastModifiedBy and needed to use JavaCast. [Update Feb 6 '10: This bug is scheduled to be fixed in 9.0.1] After making a change to myEventHandler.cfc run ormReload() (I've gotten used to having this in onRequestStart and commenting it out when not needed).
The preUpdate function is very similar except just has the setLastModifiedBy and setDateModified calls. If all your entities don't have the audit properties/columns use this technique for checking if the function exists.
Lets see it in action.
Using the Team object from above:
Thats all the code needed. Here's a screenshot of the database after insert that shows all the audit data.