using Decorators with Transfer ORM

Transfer is a system that will allow you to abstract your database from your code, and use a more generic way of accessing your data (as well as a slew of other very cool things). Once it is configured, Transfer will auto-generate methods you can use to read / list / update, and save data to your (insert database software here) server.

Anyway, lets say you have a very simple table, users. You have an ID, fName, lName, etc. Your transfer config could look like this:

<br /><object name=<span class='cc_value'>"user"</span> table=<span class='cc_value'>"Users"</span>><br />   <id name=<span class='cc_value'>"ID"</span> type=<span class='cc_value'>"numeric"</span> /><br />   <property name=<span class='cc_value'>"fName"</span> type=<span class='cc_value'>"string"</span> column=<span class='cc_value'>"fName"</span> /><br />   <property name=<span class='cc_value'>"lName"</span> type=<span class='cc_value'>"string"</span> column=<span class='cc_value'>"lName"</span> /><br /></object><br />

Your object name is your alias, this will be how you refre to it using Transfer. The table is (duh) your database table name, and the ID / property fields define each column in your database. Im not gonna get deeply into the nitty gritty of instancing Transfer or anything, but with this config, Transfer will generate methods to access your data.

<br /><span class='comment'></span><br /><cfset myUser = transfer.get(<span class='cc_value'>"user"</span>, userID) /><br /><br /><span class='comment'></span><br /><cfset myUser.setfName(<span class='cc_value'>"Harry"</span>) /><br /><cfset myUser.setlName(<span class='cc_value'>"Smith"</span>) /><br /><br /><span class='comment'></span><br /><cfset myUser.save() /><br />

Now, on to decorators =)

In case you dont know, the documentation says this about a decorator: "A decorator is used when you wish to write your own CFC to be used to represent data, in place of the Transfer generated Object."

Lets change 1 line in our transfer configuration:
<br /><object name=<span class='cc_value'>"user"</span> table=<span class='cc_value'>"Users"</span> decorator=<span class='cc_value'>"some.path.to.a.cfc"</span>><br />

Now, any methods we have inside of our some.path.to.a.cfc will be included when you create an instance of your user record! You can even over-write built in functions, replacing or supplementing them with your own. You could add a getInvoices() function that, once your user is instances and loaded (using get), you can call getInvoices() to return all invoices for the instanced user. Or, you can build additional functions over top of auto-generated transfer ones. I am digging Transfer, and I love how extensible it is!
Digg StumbleUpon Facebook Technorati Fav newsvine reddit FARK Google Bookmarks
  1. Andy J

    #1 by Andy J - March 6, 2008 at 10:19 AM

    Good post. But your code is hard to read on the mac. I don't know if you've seen http://www.coldfusionjedi.com/index.cfm/2007/7/26/Important-Update-for-BlogCFC-Users
  2. Justice

    #2 by Justice - March 6, 2008 at 10:19 AM

    Thanks Andy, I applied the fix! =)
  3. John Farrar

    #3 by John Farrar - April 28, 2008 at 5:05 PM

    OK... so we have to \"hard code\" decorator class paths? (Is there any way to do the transfer.xml file as transfer.cfm to allow some dynamic configuration?)

Comments are closed.