官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 张家港市| 林周县| 唐山市| 图们市| 宿州市| 陕西省| 桐梓县| 乐至县| 松阳县| 新河县| 名山县| 宝山区| 嘉祥县| 宜黄县| 南木林县| 龙州县| 玉门市| 长宁区| 洪雅县| 特克斯县| 安国市| 张家口市| 滁州市| 濮阳县| 仪陇县| 紫金县| 同心县| 垣曲县| 济宁市| 长沙市| 盐津县| 滦南县| 墨玉县| 乌拉特中旗| 潞城市| 辽阳县| 林西县| 墨竹工卡县| 镇原县| 南汇区| 嘉峪关市|