ColdFusioning.com
Pronunciation \kold-fy-zhn-ing\ Noun: The actions of one that writes ColdFusion.

Please check out my new site: http://ja.mesbrown.com/

Introduction to ColdFusion ORM

After coming back from MAX and seeing how wonderful ORM is, I had to jump right into it. I'm using ORM for the Tour de ColdFusion so I will post some code samples as I write them. The first set is an introduction to ColdFusion ORM.

The first thing you have to do is turn ORM on. You do this simply by adding the two following lines to your Application.cfc:

view plain print about
1<cfset this.datasource = "yourDataSourceName" />
2<cfset this.ormEnabled = true />

The DataSource name must match the one you created in the ColdFusion Administrator that is correctly pointed at a database. The next thing we'll do is define our table. We're going to call this one Categories.cfc:

view plain print about
1<cfcomponent persistent="true">
2 <cfproperty name="CategoryID"
3         type="numeric"
4         validate="integer"
5         setter="false"
6         hint="I am the unique ID of the property."
7         persistent="true"
8         fieldtype="id"
9         column="categoryid"
10         ormtype="integer"
11         generator="identity"
12         length="10"
13    />

14 <cfproperty
15        name="CategoryName">

16 <cfproperty
17        name="ParentID">

18 <cfproperty
19        name="theOrder">

20 <cfproperty
21        name="Deleted">

22</cfcomponent>

This defines the Unique ID named as Category that auto-increments. We defined some other simple columns. Next we're going to insert some data into the database:

view plain print about
1<cfscript>
2    newObj = EntityNew("Categories");
3    newObj.setCategoryName("UI Controls");
4    newObj.setParentid("2");
5    EntitySave(newObj);
6    writedump(newObj.getCategoryID());
7    ormflush();
8
</cfscript>

All this does is set the category name, parentid and saves it. It also retrieves the new CategoryID from the database. The last thing we're going to do is get some data back out of the database:

view plain print about
1<cfscript>
2 categories = EntityLoad("categories", true);
3 writedump(categories);
4
</cfscript>

This is very similar to doing except ORM loads and array rather than a query:

view plain print about
1<cfquery name="getCategories">
2SELECT * FROM Categories
3</cfquery>
4<cfdump var="#getCategories#" />

Lastly, if you want to select a single record from the database you would use this code:

view plain print about
1<cfscript>
2 categories = EntityLoad("categories", 5, true);
3 writedump(categories);
4
</cfscript>

With "5" being the CategoryID of the record you want to pull from the database.

ORM is truly a game-changer in the web development industry. It is going to enable ColdFusion developers to create applications even faster than we could before. I would recommend not waiting to learn it because it is only going to save you time in the long run.

TweetBacks

Comments

Recent Comments

RSS

Subscribe