So you want to create a CFWheels application? (Part 6)
Published on
This CFWheels series is heavily borrowed from Dan Wilson's "So You Want to" series about Model Glue:Unity and matches to this post. Previously in this series, we installed CFWheels, discussed some concepts in CFWheels, added our basic flow and navigation, created add and list functionality, added validation, and talked in more detail about the CFWheels ORM. Here is a zip, if you want to start from this post. Unzip it in an empty webroot. Today, we will cover the CFWheels ORM a little more. Specifically the return differences between some built in ORM calls and the logic why, specify the SQL order by clause and other sql fine-tuning, and build update and delete functionality. Let's create an edit action in /Models/contact.cfc:
<cffunction name="edit">
<cfset newContact = model("contact").findByKey(key=params.key, include="type")>
<cfset newContact1 = model("contact").findOne(where="id=#params.key#", include="type")>
<cfset types = model("type").findAll() />
<cfdump var="#newContact1#"><br>
<cfdump var="#newContact#"><br>
<cfdump var="#types#">
<cfabort>
</cffunction>
<th>Actions<th>
<td>#linkTo(text="Edit", action="edit", key=allContacts.id)#</td>
URL Rewriting On = http://localhost/contact/list
URL Rewriting Partial = http://localhost/index.cfm/contact/list
URL Rewriting Off = http://localhost/index.cfm?controller=contact&action=list


<cfset newContact1 = model("contact").findOne(where="id=#params.key#", include="type")>
<cfset newContact = model("contact").findByKey(key=params.key, include="type")>
FindByKey()
FindOne()
FindAll()
<cfdump var="#newContact1#"><br>
<cfdump var="#newContact#"><br>
<cfdump var="#types#">
<cfabort>
<cfset allContacts = model("contact").findAll(include="type") />
<cfset allContacts = model("contact").findAll(include="type",order="name") />

#linkTo(text="Delete", action="delete", key=allContacts.id)#
#linkTo(text="Edit", action="edit", key=allContacts.id)#
<cffunction name="delete">
<!--- delete will return true or false depending on success --->
<cfif model("contact").findByKey(params.key).delete()>
<cfset flashInsert(success="Contact #params.key# was deleted.")>
<cfelse>
<cfset flashInsert(error="There was an error deleting the contact.")>
</cfif>
<cfset redirectTo(action="list")>
</cffunction>
