Over the past month or so, I have really been digging in to ColdFusion’s ORM. It’s a fantastic tool - no longer do I have to write a bunch of boilerplate queries for every object in my codebase, nor do I have to mess around with generating complicated SQL queries by hand. That being said, it has been a relatively steep learning curve for me and I’ve been asking loads of questions every step of the way. Through the answers to those questions, I’ve managed to put together a few quick tips.
I Subscribed to the cf-orm-dev Google Group
If you are developing CF ORM applications and you are not subscribed to this group, do it now. The guys on here are awesome and very, very responsive. I had two questions yesterday which were both answered within the hour. Go sign up.
I Stopped Using dropcreate
One of the projects I’m working on is a FW/1 application that is integrating with some legacy code we have lying around. I had to map a few existing tables as entities in my application, which was no problem. I set up all my CFCs, hit
ormReload(), and then watched as I wiped out three tables full of data because my ORM
dbcreate setting was set to
dropcreate. Of course, I was on a development server, but I still had to go re-download the production database and set it up locally again. I love that I can make tweaks to a persistent entity, run
ormReload(), and see the changes happen in the databse - it’s magical! However, that wasn’t an option for this project; I didn’t want to set up the database every time I needed to make a tweak to the table layout.
I posted a question to cf-orm-dev - “Can I exclude a mapped table from the dropcreate setting?” The answer was a resounding “no,” and I thought that I was just going to have to make my DB changes the “hard” way and write some SQL by hand. Luckily, Seth Johnson came to my rescue and recommended that I set my
dbcreate setting to
update and simply write a function to drop only the tables I wanted to. Brilliant! It works perfectly, here’s the relevant bits of
As you can see, I have two URL strings that I search for in
onRequestStart(): “ormDropCreate” and “ormReload.” I think they’re pretty self explanatory - I just hit my application with http://www.mysite.com/index.cfm?ormDropCreate=true and I’ve rebuilt the parts of my database that I need to.
I Started Using Very Specific Mappings
Another project I’m working on is a Mura plugin for a client. I’ve got another ORM-using plugin on the same Mura install, but I didn’t even consider that they would have issues with each other. Well, after running an
ormReload(), CF threw an incredibly helpful error message at me:
Long story short, the error turned out to be caused by my mappings being too generic. Rather than using
plugins.SeansAwesomePlugin.model.beans.ModelName for the “cfc” attribute definition, I was just using
ModelName. I’ve now gone through all of my persistent CFCs and updated the paths to be very specific. I haven’t had any mapping-related errors since.
I Bought John Whish’s Book
The appropriately titled ColdFusion ORM Book is an excellent reference. John covers a lot of ground in 178 pages, but everything he writes is easily digested. Googling for answers to ORM related issues has made it onto my list of top-ten most painful things in the world, and John’s book is a perfect remedy. There are a lot of great tips and tricks in addition to the basics. I would highly recommend this book to anyone who is serious about getting into CF ORM development.
I am not affiliated with John Whish in any way, I just think his book is awesome.
Although ORM was painful to get started with, it is really growing on me. If you have the leeway to use it on your next project, I say go for it!
I’d love to hear about any other tips and tricks you would add to this list, so feel free to leave a comment. :)