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:
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:
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.