CF Wheels ORM Fun!!!
Posted by Stephen Weyrick | Tags: cfwheels , coldfusion , framework
Today I wanted to have some fun, so I decided to check out the CF Wheels Framework inspired by Ruby on Rails. I've never used Ruby on Rails before, but this framework is pretty nice. It took me all of about ten minutes to have a default page up and running + diplaying data from my model. I wanted to demonstrate just how easy it is to populate tabular data using the framework.
If you haven't already, you need to download the framework from cfwheels.org, then extract the zipped file into a folder of you're choosing. The first time you run the application, you should see a CF Wheels Intro page. I'm not going to go over any installation or boilerplate stuff; you can find everything you at cfwheels.org; they have some great video tutorials.
The first thing I did was create my database, it just includes a single table for now:

CF Wheels is convention based, so the database name needs to be plural. We need to do this, so Wheels can properly setup mappings between our database and model. (just wanted to point out that this not a requirement, you can override pretty much all the default settings)
Let jump into some code:
- Setting up our datasource name (config/settings.cfm)
- Setting up our model (model/[YourCFC.cfc])
- Connecting you're model to you're controller (controllers/[YourController.cfc])
- Connecting our view to our controller (views/[YourView.cfm])
<cfset set(dataSourceName="dsnPractice") />
As you can see in the example below, I didn't follow all of the conventions for Wheels, so I had to override the default properties for my table name . I also set a property for my column name, just to show you that you can override default settings. If you had more than one table and some relationships, you would also define those in here using associations.
<cfcomponent extends="model" output="false">
<cffunction name="init">
<cfset table("ParlayStats_MLBSchedule") />
<cfset property(name="scheduleID", column="scheduleID") />
</cffunction>
</cfcomponent>
So to get all the records back from my model, we would call model("schedule").findAll(). There are a ton of built in functions for displaying records anyway you like. You can find those methods here. I also would like to point out that you should have a controller for each folder inside of you're views directory. Mine is setup like so; views/external/home.cfm. So I have a controller named External with a home function inside of it. Everything that I set in my controller + method home will be available inside the home view:

<cfcomponent extends="controller" output="true">
<cffunction name="home">
<cfset scheduleRow = 1 />
<cfset title = "Parlay Stats">
<cfset scheduleQry = model("schedule").findAll() />
</cffunction>
</cfcomponent>
In our view we would want to make sure to cfparam the query being passed to it, and then just output those records in a cfloop.
<cfparam name="scheduleQry" type="query" />
<dl class="schedule">
<dt>Giants Schedule</dt>
<cfloop query="scheduleClass">
<cfoutput>
<cfif #scheduleRow# mod 2>
<dd class="even"><a href="">#scheduleQry.gameNames#</a><span>#lsDateFormat(scheduleQry.startDates,'dd mmm yyyy')#</span></dd>
<cfelse>
<dd><a href="">#scheduleQry.gameNames#</a><span>#lsDateFormat(scheduleQry.startDates,'dd mmm yyyy')#</span></dd>
</cfif>
<cfset scheduleRow = #scheduleRow# + 1 />
</cfoutput>
</cfloop>
</dl>
Final Result:

6 responses so far ↓
Leave a Comment