So you want to create a CFWheels application? (Part 6)
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 ,...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
Click on an Edit link and you should see this:
What you see here is two objects and one query. You may think this is odd since you are using the CFWheels built in ORM and you thought the ORM calls would return the same structure. I thought the same thing but when talking to Per Djurner, it made perfect sense. Here is (http://groups.google.com/group/cfwheels/browse_thread/thread/bbe87f459b7a294c/8f4a1c09a37d1814?lnk=gst&q=object+query#8f4a1c09a37d1814): "The convention is that when you are fetching a single record from the database (using findOne, findByKey etc) you will get an object back.When you are fetching multiple records you will get a query result set back. The "returnAs" argument is a way to override this convention (and it will be improved a little more before we release 1.0)." "The reasoning behind the convention is that when you are asking the database for multiple records it is likely that you intend to display them (rather than edit/delete them). In this case objects are overkill (and even more so because of the poor performance of object creation in ColdFusion)." See the CFWheels guys are always thinking. Don't be afraid to ask @ the (http://groups.google.com/group/cfwheels). One other thing, I would like to point out is <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") />
See the SQL now has an "Order By" clause. Lets wrap this entry up by adding delete functionality. Go to /Views/list.cfm, and add #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>
Success! For more (http://cfwheels.org/docs/chapter/deleting-records) We have a fully working Contact-O-Matic Application. Next in the series, I'll talk about Routing and Plugins in CFWheels.