26
Feb

Wow! Coldfusion ORM

A few times a year I like to pick a technology that I am not as well versed in and develop a mini application. I do this to learn ins and outs and determine if it’s worthy of using in my day to day development work. Since all of us developers have done our fair share of CRUD (Create, Read, Update and Delete), I have always been interested in Coldfusion ORM. The problem really was that it seemed really heavy handed. The tutorials and documentation I found always explored examples that created hard to work with, data heavy arrays.

This year I decided to really set down and make a final analysis of Coldfusion ORM. My conclusion? Its freaking awesome! If you can get past the initial learning curve you will discover some very powerful tools. I do not see ORM completely replacing my current coding approach but I will certainly be leveraging it where best applicable.

Since it was rather difficult finding a good learning source online, I am thinking that I would like to post a series of brief explorations of Coldfusion ORM features that I find helpful.  I hope you will find them helpful and I encourage you to comment .

Before I begin, I would like to point you to John Wish’s Book. I would recommend it to any Coldfusion developer.

In this post I would like to explore the basic Application.cfc settings that will get you started.

 

A bit of this will look familiar.

 

Line 2: THIS.name - As with all our applications we need to come up with a name for the application.

Line 3: THIS.datasource - Coldfusion ORM needs to know the datasource it will use.  Setting the datasource has the added benefit of allowing us to skip writing “datasource=”datasourceName” when using cfquery. THIS.datasource sets the default datasource for all the application's queries.

Line 4: THIS.rootDirectory - Is just a way to reference the directory root. You could call it THIS.bananas if you wanted. The variable name is up to you.

Line 5: THIS.mappings - Allows us to more easily map to the location of our components regardless of what template is calling it.

Line 6: THIS.ormEnabled - This is the first completely ORM related setting. This setting turns on ORM for the application.

Line 7: THIS.ormSettings - This is where all the fun begins. As you can see, ormSettings is a structure containing optional settings. There are many elective settings here. For a full list go to the documentation for whatever CFML engine you are using.

cfclocation is used to help ORM locate the persitent components that drive your ORM application. 

Setting dbcreate to update will add new columns and tables but will not alter existing tables/columns. Setting dbcreate to dropcreate will drop all the tables and recreate them. Dont worry, this action only takes place when you reload your ORM application (ormReload()). Be careful not to leave dropcreate in a production enviroment since it will erase any data contained in your database. Setting dbcreate to none is the default and will not alter the db in any way.

I like to set the namingstrategy to smart. This creates table/column naming in a way that incompases most database convensions. If you dont know which database you will deploy to then this is a safe bet. Smart changes camel case to all capitals and puts an underscore before each capital within the word. For example, coldFusion becomes COLD_FUSION. This setting is optional. The default will not alter your table/column names.

One setting that I find very helpful is sqlscript. This allows you to populate your database with data that has been prewriten and loaded via a sql script. For example, you may want to alway put in an admin user into your users table. All you have to do is include the INSERT script for this in your sqlscript file and it will automatically insert upon running ormReload(); Ex. Create file and name it sqlscript.sql and add your version of the following:  INSERT INTO table_name (column1, column2) VALUES (column1Value, column2Value);

Tags: ORM; Coldfusion