Create, Retrieve, Update, and Delete in ColdFusion

Mike has a great little tool running over on his site that does creates crud for you (where CRUD stands for Create, Retrieve, Update, and Delete). In another sense, he’s a couple steps away from creating an object-relational mapping tool similiar to what some open source Java tools do.
Mike, go one step higher and think about your data as an object and dynamically create CFC’s with the proper methods for creating, retrieving, updating, and deleting. This would mean that other developers using your system could use code that looks like this to create an event (or news or press release… given that MB’s system first created a table for an event and then created a CFC called event):

myEvent = createObject(“component”, “com.michaelbuffington.event”);
myEvent.setId(variables.id);
myEvent.setLabel(form.label);
myEvent.setStartDate(form.startDate);
myEvent.update();

Now other developers on your team a) don’t have to know what the names of the db tables are, b) you don’t have to worry about having multiple update queries (some of which might be buggy) and c) it’s just plain easier than writing

<cfquery name=”updateEvent” datasource=”#request.dsn#”>
update event set label = ‘#form.label#’, startdate = #form.startdate#
where id = #form.id#
</cfquery>

Other benefits to using a CFC that does your create(), update() and delete() for you: a) you can insert logging statements into your delete() and update() methods so that all updates and deletes are logged to the file system. I had a system setup like this on the footjoy.com project I worked on this past summer. It came in really handy when some of the content editors began putting in content… in fact they created over 100 pieces of content and then saved that content. Only the update() statement wasn’t working and wasn’t throwing a visible error to them. In my log file I had update statement so I only needed to parse out the bad code from the log file and drop that into Query Analyzer to get back all the updates. b) you can handle deleting objects on the file system that might be related to that piece of content. For instance, many press releases have a PDF file associated with them. If you used this:

myPressRelease = createObject(“component”, “com.michaelbuffington.pressrelease”);
myPressRelease.delete();

you could make sure that the press release PDF file was deleted from the file system at the same time that the press release is deleted from the database….

Anyway…. great job Mike! Looks like a great tool!

2 thoughts on “Create, Retrieve, Update, and Delete in ColdFusion”

Leave a Reply

Your email address will not be published. Required fields are marked *