Posts Tagged ‘ORM’

Mapping a table using ORM in Coldfusion 9

Saturday, March 13th, 2010

I’ll assume here that you have already set ORM in your Application.cfc.  If not you may want to check out the post on setting up ORM in the Application.cfc file first.  If you have the Application file all set and you have a database then you’re ready to map out a database table.

For this example I’ll be using MySql as the database with a table called ‘stores_tbl’.  It has three fields that we need to worry about.  The first is the ‘store_id’ (the primary key), the ‘store_name’, and the ‘store_link’.

We’ll need three files to make this work.  First is the Application.cfc file, which again I’m assuming is already set up.  Second is a .cfm file that will output our data objects.  And third is the persistent cfc that will map to the ‘stores_tbl’.  I’ve created a directory called ‘orm_mappings’ under my root directory to hold all my persistent cfcs.   You can also create a directory if you like.  When the application loads Coldfusion searches through the application looking for any persistent cfcs and maps them accordingly.  So technically you could organize your persistent cfcs any way you wish as long as they are inside the application using ORM.

Let’s take a look at our persistent cfc.

stores_tbl.cfc

<cfcomponent displayname="Stores Table" hint="ORM mappings to the stores_tbl" output="false" persistent="true">

	<!--- SET PRIMARY KEY COLUMN --->
	<cfproperty name="id" column="store_id" ormtype="integer" generator="native">

	<!--- SET OTHER COLUMNS --->
	<cfproperty name="store_name" ormtype="string">
	<cfproperty name="store_link" ormtype="string">

</cfcomponent>

First thing to note here is the name of the cfc. It is named the same as the table name it is mapping to. So our table name is ‘stores_tbl’ and the name of the cfc is ‘stores_tbl.cfc’. The name of the cfc will indicate to Coldfusion where the mapping is to take place. Next is the ‘persistent=”true”‘ attribute in the ‘cfcomponent’ tag. This indicates to coldfusion that this is an ORM mapping and not a standard cfc.

(more…)

Setting up ORM in Coldfusion 9

Saturday, March 13th, 2010

First things first, you will need a database and Coldfusion 9 for this to work.  Once you have  these two things you’ll need to create your Application.cfc file.

Remember in Coldfusion 8 using the THIS scope at the top of the component outside of any functions to set things like the name of the application, the session management, and the application timeout.  Well, now you need to add to that list a few new settings in the THIS scope.  You’ll need to add THIS.ormenabled = true and THIS.datasource = “yourdatasource”.

So our very simple Application.cfc file will look like the following.

Application.cfc

<cfcomponent displayname="Application File" hint="Application File" output="false">

	<!--- SET UP ORM --->
	<cfset THIS.name = "applicationTest">
	<cfset THIS.datasource = "datasource">
	<cfset THIS.ormenabled = true>

</cfcomponent>

Keep in mind that setting the datasource in the THIS scope is the same thing you did in Coldfusion 8 when you would set the datasource in the tag. Point being that this datasource is pretty useless without setting up the datasource in the Coldfusion Administrator first. Take a look at Setting up a datasource to learn more about this.  The really freakin awesome thing about this though is that setting the datasource here sets it globally across your application, which means for every call to the database you do not need to specify the datasource.  This is a big deal in my book.

So ORM should be all set in your application.

DISCLAIMER** This is a record of what I’m learning in ORM in Coldfusion 9. While what I post should work, I lay no claim to best practices. My hope here is that other ORM beginners like myself may benefit from this information on the assumption that their path to learning ORM might be similar to my own. To learn more about ORM by those who know more than myself check out (of course) the adobe docs and a couple very nice presentations on cfmeetup.com by Raymond Camden and Bob Silverberg.