CFWheels make the database simple

cfwheels ColdFusion

I am working on porting Litepost to Wheels. Wheels had a Litepost competition awhile ago but didn’t follow the Litepost comparison idea.Litepost was conceived to compare different frameworks using essentially the same code. Once done, you will be able to compare how the same app is done in ColdBox, Fusebox, FW/1, Mach-II, Model-Glue, and Wheels.

I am wanted to quick show how the Wheels Object Relational Mapper makes database calls very simple and intuitive.

Here is the

COLDFUSION
\Models\Category.cfc

COLDFUSION
<cfcomponent extends="Model" output="false">
<cffunction name="init">
<cfset property(name="id", column="categoryid")>
<cfset property(name="categoriesCount", sql="(SELECT COUNT(*) FROM entries WHERE categories.categoryid = entries.categoryId)")>
<cfset validatesPresenceOf(properties="category")>
<cfset belongsTo("Entry")>
</cffunction>
</cfcomponent>

See how everything about the Categories table is self contained and self documenting?Wheels is even smart enough with Wheels conventions to realize the table name for

COLDFUSION
\Models\Category.cfc
is categories.

The first properties() function is renaming a column into something more in-line with the Wheels conventions. It adds an "AS id" to the sql for the categoryid column. No more inconsistent sql commands :-)

The second property is a really wicked trick called Calculated Properties. Thanks to Raul for showing me how to do it. It is actually doing a count of all the entries matching a categoryid. Now when the category model is called like

COLDFUSION
<cfset categories = model('category').findAll() />
in the controller, it will have a new column called
COLDFUSION
categoriesCount
with the count.

The next function validatesPresenceOf() is actually part of Wheels built in Object Validation.

Another cool feature of the Wheels ORM is the deleteEntry action in the

COLDFUSION
\Controller\Blog.cfc
. Look no sql and the command will delete all the associated comments, then delete the actual entry.

COLDFUSION
aEntry = model("entry").findByKey( id );
aEntry.deleteAllComments();
aEntry.delete();

How does it know to create a

COLDFUSION
deleteAllComments()
function on the
COLDFUSION
aEntry object
? It was told in the
COLDFUSION
\Models\Entry.cfc
with a hasMany association.

COLDFUSION
<cfcomponent extends="Model" output="false">
<cffunction name="init">
<cfset property(name="id", column="entryid")>
<cfset validatesPresenceOf(properties="title,body")>
<cfset hasMany("comments")>
<cfset hasOne("category")>
</cffunction>
</cfcomponent>

I could even create a Callback so when a delete of an Entry anywhere, it automatically deletes any related comments.