CFWheels and ORM

  Dec 14, 2009     12926 Views      ColdFusion       Comments (1)   

Object Relation mapping is one of the key feature in CFWheels. CFWheels makes things easier to start using ORM in your applications. The ORM configuration in CFWheels is implemented by creating a mapping between a database table and ColdFusion component in your application. To demonstrate ORM feature lets create a simple use case that list the available categories in a table with edit and delete links along a with add new category option.

First set the datasource name in settings.cfm file under config folder.

<cfset set(dataSourceName="CFWheelsORMDSN")>

I'm using blogCFC database for this example and to map the table tblblogCategories, I have created a CFC under the model folder in my CFWheels framework and I named it as tblblogCategory.cfc. The thumb rule of ORM mapping in CFWheels is that the CFC name should be a singular version of your table name.

<cfcomponent extends="Model">

Also CFWheels gives us the flexibility of creating CFC's with other names as well to map a table. Here I have created CFC as category.cfc and to map the tblblogCategories here is the code I need to write.

<cfcomponent extends="Model">
   <cffunction name="init">
      <cfset table("tblblogcategories") />

One important point to note here is all the CFC's you're creating for database tables mapping should need to extend the Model component. We have done with the mapping now let's create some functions to perform CRUD operation in tblblogCategories table. Here is the code of the controller CFC CFWheelsORM.cfc.

<cffunction name="createCategory">
   <!--- calling the new() method --->
   <cfset objCategory = model("tblBlogCategory").new() />
   <!--- setting values to Category Object --->
   <cfset objCategory.categoryid      = createUUID() />
   <!--- dynamic ID for Category name --->
   <cfset dCatID            = ListGetAt(objCategory.categoryid , 2, '-') />
   <cfset objCategory.categoryName    = "CFWheelsORM" />
   <cfset objCategory.categoryAlias    = "CFWheelsORM" />
   <cfset          = "Default" />
   <!--- calling the save() method to save the content to DB--->
   <cfset />
   <cfset redirectTo(action="getCategory") />
<cffunction name="getCategory">
   <cfset qGetCategory = model("tblBlogCategory").findAll(
               SELECT="categoryid, categoryname",
               WHERE="categoryName LIKE 'C%'",
               ORDER="categoryname") />

<cffunction name="editCategory">
   <cfset objCategory = model("tblBlogCategory").findByKey(params.key) />
   <!--- dynamic ID for Category name --->
   <cfset dCatID      = ListGetAt(params.key , 3, '-') />
   <cfset objCategory.update(categoryName="CFWheels") />
   <cfset redirectTo(action="getCategory") />
<cffunction name="deleteCategory">
   <cfset objCategory = model("tblBlogCategory").findByKey(params.key) />
   <cfset objCategory.delete() />
   <cfset redirectTo(action="getCategory") />

I'm not using any form controls in this CRUD example hence I have used the values to createCatetory and editCategory as static. In the createCategory function I have created the new instance for my tblBlogCategory Model and passing the necessary params to it for to create a new category and calling the save() method to store the values to my database. In getCategory function I used the findAll() method and passing my SQL statement to it. If we didn't pass any SQL String then findAll() method will return all records from the table. By default findAll() method will return a query object we can also change this behavior to get an array of objects by setting like below. This will be very handy in many cases.

<cfset qGetCategory = model("tblBlogCategory").findAll(returnAs="objects") />

In editCategory and deleteCategory functions I have used another ORM function findByKey() as name denotes it will use to fetch data based on the parameter we have passed to it. The Update() and Delete() methods are used to update and delete data's from the database. CFWheels automatically convert all the inputs we passed into to the ORM methods to CFQueryParam. Like the Controller file name we need to create a folder under the CFWheels framework's view folder and then create new view file manageCategory.cfm in it with the following code.

<cfoutput query="qGetCategory">
   [ #linkTo(text="Edit", key=qGetCategory.categoryID, action="editCategory")# |    #linkTo(text="Delete", id=qGetCategory.categoryID, action="deleteCategory")# ]

   #linkTo(text="Add", action="createCategory")#

I used the linkTo() function to create a link in my view page which links to the editCategory and deleteCategory actions by passing the categoryID as a key. We have done with a very basic ORM example in CFWheels and to learn more about CFWheels ORM features read the documentation at CFWheels site.

Related Entries


1. Kilatkyut on Dec 29, 2009 at 2:33 AM

Nice one thanks...

About Me

CFML/Web/Cloud/Data/Agile Enthusiast, Husband & Dad, an avid learner & android fan who works as a Tech Consultant in Toronto, Canada.!
Get Your Next CFML Job!
ColdFusion Tuts
ColdFusion Tutorials & Resources


Adobe Agile AppCore Creator BlogCFC Books Bootstrap CFBuilder CFEclipse CFML Chennai CFUG Chrome Extensions ColdFusion Google IIS India Learning MySQL Railo Subversion Tools & Utilities Web Windows 7


2021 2018 2017 2016 2014 2013 2012 2011 2010 2009 2008


Akbarsait © 2008 - 2023 | Hosting provided by Vivio Technologies

Home | Blog | Sitemap | RSS Feed