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:
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<cfset this.datasource = "yourDataSourceName" />
<cfset this.ormEnabled = true />
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:
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<cfcomponent persistent="true">
<cfproperty name="CategoryID"
type="numeric"
validate="integer"
setter="false"
hint="I am the unique ID of the property."
persistent="true"
fieldtype="id"
column="categoryid"
ormtype="integer"
generator="identity"
length="10"
/>
<cfproperty
name="CategoryName">
<cfproperty
name="ParentID">
<cfproperty
name="theOrder">
<cfproperty
name="Deleted">
</cfcomponent>
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:
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<cfscript>
newObj = EntityNew("Categories");
newObj.setCategoryName("UI Controls");
newObj.setParentid("2");
EntitySave(newObj);
writedump(newObj.getCategoryID());
ormflush();
</cfscript>
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:
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<cfscript>
categories = EntityLoad("categories", true);
writedump(categories);
</cfscript>
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:
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<cfquery name="getCategories">
SELECT * FROM Categories
</cfquery>
<cfdump var="#getCategories#" />
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:
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<cfscript>
categories = EntityLoad("categories", 5, true);
writedump(categories);
</cfscript>
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.
Comments
There are no comments for this entry.
[Add Comment] [Subscribe to Comments]