ColdFusion 9 cfgrid with ORM: Part 1

One of the exciting new feature in ColdFusion 9 is its ORM implementation. ORM in simplest terms allows us to access the database using objects, or in in even more simpler terms you don't have to write any queries for simple CRUD functions. To learn more about ColdFusion 9 ORM visit Adobe's ColdFusion 9 ORM Docs and Introducing ORM in ColdFusion 9 Beta.

This post will look at implementing a cfgrid using ORM. This will be first in a series starting from just implementing the grid to read data ending with a full fledged CRUD cfgrid with ORM.

The first thing we do is setup our Application to use ORM and then setup our Persistent CFC mapped to the Users table.
Application.CFC:

   view plainprintabout
 <cfcomponent output="false">
     
     <cfscript>
         
         this.name = "CF9 Examples";
         this.sessionmanagement = true;
         
         this.ormenabled = true;
         this.datasource = "MainApp";
10          //ORM Settings
11  
        this.ormsettings.cfclocation = "Logic";        
12          this.ormsettings.dialect = "MicrosoftSQLServer";
13          this.ormsettings.dbcreate = "update";
14                  
15      
</cfscript>    
16  
17  </cfcomponent>

At the basic level, we tell the applictation to use, all CFCs dealing with ORM will be in a subfolder called Logic (from the application root). Setting the dialect is optional. Also, I don't recommend doing this with MS Access, so I am using SQL Server for the ORM examples.

Users.cfc:
   view plainprintabout
 <cfcomponent persistent="true" table="Users" entityname="Users">
 
     <cfproperty name="ID" hint="User ID" fieldtype="id" type="numeric" datatype="integer" generator="identity">
     <cfproperty name="UserAccountingCode" type="string" length="255">
     <cfproperty name="FirstName" type="string" length="255">
     <cfproperty name="LastName" type="string" length="255">
     <cfproperty name="Phone" type="string" length="12">
     <cfproperty name="Fax" type="string" length="255">
     <cfproperty name="Email" type="string" length="255">
10      <cfproperty name="UserName" type="string" length="255">
11      <cfproperty name="Password" type="string" length="255">
12      <cfproperty name="DisplayName" type="string" length="255">    
13  
14  </cfcomponent>

This is ORM at its simplest. We have specified the columns in the Users table and created the basic infrastructure we need to start using ORM in ColdFusion.
Next our cfm file that renders the grid, still looks the same.

   view plainprintabout
 <div align="center">
 <cfform>
     <cfgrid title="Users" name="usersgrid" pagesize="25" format="html" colheaderbold="true" colheaderfont="Verdana" colheaderfontsize="90%"
 align="middle" colheaderalign="center" collapsible="true" selectOnLoad="false"    font="Verdana" fontsize="90%" striperows="true" striperowcolor="##F0FAFF" bind="cfc:Logic.UsersAccess.getUsers({cfgridpage},{cfgridpagesize},{cfgridsortcolumn},{cfgridsortdirection})">

                 
     <cfgridcolumn name="FirstName" header="First Name" width="150">
     <cfgridcolumn name="LastName" header="Last Name" width="150">
     <cfgridcolumn name="UserName" header="User Name" width="150">
     <cfgridcolumn name="DisplayName" header="Display Name" width="150">
10      
11  </cfgrid>
12  </cfform>
13  </div>
So, to get our grid data we will call the getUsers function in the Component UsersAccess. The Users Access CFC looks like:
UsersAcess.cfc:
   view plainprintabout
 <cfcomponent>
 
 <cffunction name="getUsers" access="remote" returntype="any" returnformat="JSON">
     <cfargument name="page" required="yes">
      <cfargument name="pageSize" required="yes">
      <cfargument name="gridsortcolumn" required="yes">
  <cfargument name="gridsortdirection" required="yes" default="asc">    
      <!--- Set default sort column --->
     <cfif arguments.gridsortcolumn EQ "">    
10           <cfset sortColumn = "ID">         
11      <cfelse>
12          <cfset sortColumn = arguments.gridsortcolumn>
13      </cfif>
14      <!--- Lowercasing it, probably not required --->
15      <cfset sort = lCase(Arguments.gridsortdirection)>
16      <!--- Get all the results from the Users table. We also pass in the sorting information. The blank second argument takes in filter information --->
17      <cfset objORMUsers = EntityLoad("Users",{},"#sortColumn# #sort#")>    
18      <!--- Convert to queries and we are done --->        
19      <cfset selORMUsers = EntityToQuery(objORMUsers)>
20      
21      <cfreturn queryconvertforgrid(selORMUsers,Arguments.page,Arguments.pageSize)/>
22      
23  </cffunction>
24  </cfcomponent>

The function calls the EntityLoad() that is an ORM function that loads all the table results for us. We also pass in the sorting arguments to that function. Next Adbobe provides us the EntityToQuery() function to convert the results of the EntityLoad function to a query. We pass that into queryconvertforgrid() and we are all done. Very simple, very easy, but this will form the basis for the rest of the blog posts.

View Demo. Once again, the speeds of the CF9 demos maybe slow due to the cheap VPS hosting.
Download Source

Next we will look at starting the implementation of CRUD functionality to this grid using ORM. Coming Soon.

Update: ORM Demos are currently unavailable.

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Maertsch's Gravatar Demolink doesn't work.
# Posted By Maertsch | 7/23/09 4:05 AM
Kumar Shah's Gravatar @Maertsch, it should be working now.

Had to change CF9 demo hosts.

http://cfria.coldfusion9.beta.hostek.com/ORM/gridt...
# Posted By Kumar Shah | 7/23/09 11:31 AM
John Allen's Gravatar Just found your blog, like your topics.
# Posted By John Allen | 7/23/09 8:37 PM
sam's Gravatar thanks for th euseful article and source samples, your own demo is still broken
# Posted By sam | 1/25/10 4:36 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.5.003.  Design based on ARCLITE by: digitalnature