CF Wheels ORM Fun!!!

CF Wheels LogoToday 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:

Database Design

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:

  1. Setting up our datasource name (config/settings.cfm)
  2. <cfset set(dataSourceName="dsnPractice") />

  3. Setting up our model (model/[YourCFC.cfc])
  4. 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>

  5. Connecting you're model to you're controller (controllers/[YourController.cfc])
  6. 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:

    File Structure

    <cfcomponent extends="controller" output="true">
       
        <cffunction name="home">
           
            <cfset scheduleRow = 1 />
           
            <cfset title = "Parlay Stats">
           
            <cfset scheduleQry = model("schedule").findAll() />

        </cffunction>

    </cfcomponent>

  7. Connecting our view to our controller (views/[YourView.cfm])

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:

Tabular Data

 

 

 

 

 

 

 

 

 

 

6 responses so far ↓

Joel Hill - Feb 22, 2010 at 12:39 PM

Awsome stuff!

Super easy. I'm loving cfwheels so far!

Raul Riera - Feb 22, 2010 at 12:47 PM

Pretty nice, glad to see that you enjoyed the framework... just a quick note...you don't need to make your columns in plural, only the database table should be named that way (and can be overwritten of course)

For a full list you should check out this page http://cfwheels.org/docs/chapter/conventions

Stephen Weyrick - Feb 22, 2010 at 1:18 PM

@Raul

Thanks for pointing me in the right direction, I should probably double check the docs before making my next post.

John Gag - Feb 22, 2010 at 3:47 PM

Great post Steve. We are going to have to play around some more with this and I look forward to you showing me some things.

PS. Killer Mango Theme! ;)

Chris Peters - Feb 26, 2010 at 9:03 PM

Nice writeup, Stephen.

Rather than having a <cfif> block that looks like this:
<cfif #scheduleRow# mod 2>

Try this in your HTML element instead:
<dd class="#cycle("odd,even")#"><a href="">#scheduleQry.gameNames#</a><span>#lsDateFormat(scheduleQry.startDates,'dd mmm yyyy')#</span></dd>

Here's documentation for the cycle() function:
http://cfwheels.org/docs/function/cycle

I know you're just starting out. But there's all kinds of little things like that that you'll learn over the coming weeks if you stick with it. ;)

Stephen Weyrick - Feb 27, 2010 at 11:20 AM

@Chris

Thanks for the pointer. I have already learned a lot just playing around with wheels; I think its a really nice framework, and I am really digging the fact that it has ORM built in.

Leave a Comment