- ColdFusion 9 Developer Tutorial
- John Farrar
- 437字
- 2021-08-05 16:16:41
Let's look under the hood
Well, since we are focusing on reusing and learning CFCs, it would be great if we could move our logic from our page into the CFC. You can have a look at Chapter 2, Basic CFCs and Database Interaction, if you want to see the CFC that we were building for the product table. A constructor is used to get a CFC set up for use. Some CFCs do not require it, while others do. Obviously, this one requires a constructor as it has an attribute that is required, dsn
. We have the following:
<cfcomponent output="false" extends="_sdo"> <!--- Constructor Methods ---> <cffunction name="init" access="public" output="false"> <cfargument name="dsn" required="true"> <cfargument name="id" default="0"> <cfscript> variables.field.name = "id,name,description,price"; variables.field.defaults = "0,'','',0"; variables.field.allowNull = "0,0,0,0"; variables.table = "product"; variables.pKeyField = "id"; variables.pKeyNew = 0; variables.pKeyType = "autoInteger"; variables.dsn = arguments.dsn; setDefaults(); </cfscript> <cfreturn this /> </cffunction> </cfcomponent>
We can see that all of the extra methods that are being used are actually inside a parent CFC called _sdo.cfc
. We are inheriting the methods of the parent CFC. By refactoring our common logic into a common CFC, we can have simpler CFCs for our data objects. This CFC is known as a Simple Data Object. Refer to the book's site at http://books.sosensible.com.
There is an "untapped power" under the hood of our _sdo.cfc
. It also has a save()
method and a saveStruct()
method. If you are manually setting the protected attributes, one at a time, then the save()
method will store the data for you. If you are taking a FORM or URL structure that has the values that we want to store, then use the saveStruct()
method. This will be similar to the variable names with the fields in your simple data object. Now, store them by passing in one structure variable collection. Let us make some changes to our edit page.
First, we need to remove the last set of query sections that we had added previously and then put some simpler code onto our page. Then, we will move the logic for saving records so that it works with our power CFC. We will then check if a message exists and redirect to the new page once the form is submitted:
<!--- Processing ---> <cfinclude template="request_attributes.cfm"> <cfparam name="attributes.id" default="0"> <cfscript> myRedirect = ""; objProduct = createObject("component","product").init(dsn="cfb"); if(structKeyExists(attributes,"submit")) { result = objProduct.saveStruct(attributes); myRedirect = "product_list.cfm?message=#result.message#"; } else { objProduct.load(attributes.id); } </cfscript> <cfif myRedirect NEQ ""> <cflocation url="#myRedirect#"> </cfif>
Note
There is a wonderful ORM-like tool that is great for people getting started called Data Manager. ORM means an object-based relational manager for data. It is an alternative to the ORM features in CF9. If you need an ORM-like tool that works backwards compatible with previous versions of ColdFusion, this is a great solution. You can find this library listed in the ORM section in the appendix.
- AI繪畫教程:Midjourney使用方法與技巧從入門到精通
- Oracle Business Intelligence : The Condensed Guide to Analysis and Reporting
- ERP沙盤模擬教程
- Photoshop CS6從入門到精通
- CorelDRAW X5實(shí)用教程(第2版)
- Joomla! Social Networking with JomSocial
- Moodle JavaScript Cookbook
- Photoshop+Adobe Camera Raw+Lightroom(攝影后期照片潤(rùn)飾實(shí)戰(zhàn))
- Photoshop CC從入門到精通(全彩超值版)
- Creo 4.0從入門到精通
- UI功夫:PC和APP界面設(shè)計(jì)全流程圖解
- Implementing SugarCRM 5.x
- 中文版Photoshop CC基礎(chǔ)培訓(xùn)教程
- Photoshop CS6數(shù)碼照片處理入門到精通
- 24小時(shí)學(xué)會(huì)Word-Excel-PowerPoint 2010三合一