Cfwheels : Sample application
ColdFusion on Wheels is a CFC based framework using the MVC pattern. It has built-in support and auto detection of SQL Server 7+, Oracle 10g+, and MySQL 5, the SQL statements are created for you, so all you need to do is set up your table relationships in code and follow some simple naming conventions.
The weakest part of ColdFusion on Wheels is the documentation (I don't blame the guys as I don't like writing documentation) so I decided to build a really simple example of getting and displaying data from a database, hopefully this will help others. I should point out that I am not an expert, this is just me learning the framework. The core files can be downloaded from the ColdFusion on Wheels website.
In the Models directory I added two CFCs, one for each table in my database. One called Model.cfc and one called Comment.cfc. Notice that the names of the CFCs are singular. Table names in the database must also be plural. This is one of the naming conventions that the framework uses (although it can be overwritten).
My Posts (note the plural!) database table has a one-to-many relationship with the Comments database table. In other words a post can have many comments. The set this up you CFCs need to look like this:
Post.cfc
<cfcomponent extends="model">
<cffunction name="init">
<cfset hasMany("Comments") />
</cffunction>
</cfcomponent>
Comment.cfc
<cfcomponent extends="model">
<cffunction name="init">
<cfset belongsTo("Post") />
</cffunction>
</cfcomponent>
That's it - you have now established the relationship. In fact, in this sample application you don't even to set up the relationships (remove the init methods) as I'm not using them but it is worth doing for future use.
The Controllers directory is where you set up which URLs map to which CFCs and methods. For example http://localhost.cfwheels.com/index.cfm/main/listposts will call Main.cfc and invoke the listposts method. I haven't explained that very well but I hope it kind of makes sense! So let's look at Main.cfc.
<cfcomponent extends="Controller">
<cffunction name="welcomeToWheels">
<cfset title = "Welcome To Wheels" />
<cfset version = application.wheels.version />
</cffunction>
<cffunction name="listposts">
<cfset title = "All Posts" />
<cfset qPosts = model("Post").findAll(order="published desc") />
</cffunction>
<cffunction name="viewpost">
<cfset oPost = model("Post").findById(id=params.id) />
<cfset title = oPost.Title />
<cfset qComments = model("Comment").findAll(where="postid=#oPost.id#", order="added") />
</cffunction>
</cfcomponent>
As you can see we have 3 methods, which equates to 3 screens. Despite the simple code, there is a lot going on here. The welcomeToWheels method is the default page which you get when you download the core files. I've added the listposts and viewpost methods.
Listposts gets all the posts from the posts table and orders them by the published date descending. This is a ColdFusion query object and I've assigned it to the variable qPosts.
Viewpost gets a specific post for the passed id (primary key). This time it returns an object, which I've assigned to the oPost variable. I then get a query object of all the comments for that post ordered by date (qComments).
In the view directory, which is where our display pages are, I added listposts.cfm and viewpost.cfm which correspond to the method names in the Main.cfc. This is another convention that ColdFusion on Wheels uses. These are straight forward.
listpost.cfm
<cfoutput>
<cfloop query="qPosts">
<h2>#linkTo(text=qPosts.title, action="viewpost", id=qPosts.id)#</h2>
<p>#qPosts.content#</p>
<p>#DateFormat(qPosts.published, "d mmmm yyyy")#</p>
<hr />
</cfloop>
</cfoutput>
viewpost.cfm
<cfoutput>
<h2>#oPost.title#</h2>
<p>#oPost.content#</p>
<p>#DateFormat(oPost.published, "dd-mm-yyyy")#</p>
<cfif qComments.RecordCount gt 0>
<hr />
<h2>Comments</h2>
<ol>
<cfloop query="qComments">
<li>
#qComments.Comment#<br />
<em>#qComments.Author#, #DateFormat(qComments.Added, "d mmmm yyyy")#</em>
</li>
</cfloop>
</ol>
</cfif>
</cfoutput>
The only thing of interest is the linkTo(text=qPosts.title, action="viewpost", id=qPosts.id) in listpost.cfm. This is a built-in function which builds your links for you. The first argument is the test to show for the link, the second argument is the method to call when the link is clicked, the third argument is the id of the post to include in your link.
Now that the code is done we need to set up and connect to a datasource. The code to build a MySQL database and populate it is in the zip. Set up your datasource in CFIDE as you normally would. Open up database.cfm in the config directory and set the datasource name. For my sample application it is cfwheels.
You need to install this in the root of your webserver (or set up a virtual host in Apache), then fire up a browser and away you go!
Download the code (cfwheels-simple.zip).
- Posted in:
- ColdFusion
- ColdFusion on Wheels


Comment by Allen – March 30, 2009
Comment by John Whish – April 02, 2009
Comment by Indy Nagpal – December 01, 2009
cfwheels.org/docs
Comment by John Whish – December 01, 2009
Till then feel free to use, nagpals.com/utils/cfwheels/search/
Comment by Indy Nagpal – December 02, 2009
Comment by John Whish – December 03, 2009