Entries Tagged as "Transfer ORM"
Using a Transfer decorator to log all database changes
Posted by [Justice] in Coldfusion , Transfer ORM on February 26, 2008
This is a quick and dirty blog entry, just wanted to hop on and blog about a method Im using to log database changes or additions done via Transfer.
All of my Transfer objects have decorators that were auto-generated by the very slick Illudium PU36 Code Generator, which basically adds a simple validate method. Normally a transfer decorator will extend transfer.com.TransferDecorator. Well, I have changed all of my decorators to extend a custom decoratorUtility, so that I can create shared functions across all transfer objects. Here is what my utility looks like:
<cffunction name=<span class='cc_value'>"save"</span> access=<span class='cc_value'>"public"</span> returntype=<span class='cc_value'>"void"</span> output=<span class='cc_value'>"false"</span> hint=<span class='cc_value'>""</span>>
<cfargument name=<span class='cc_value'>"logData"</span> default=<span class='cc_value'>""</span> required=<span class='cc_value'>"false"</span> hint=<span class='cc_value'>"Accepts a string to be logged as the 'action' instead of the default"</span> />
<cfset var className = '' />
<cfset var pkName = '' />
<cfset var pkType = '' />
<cfset var pkValue = '' />
<cfset var action = '' />
<cfset var user = getAuthUser() />
<cfset var logDate = now() />
<cfset var log = '' />
<!--<span class='cc_comment'>-// Gather some information about what we will be saving //---></span> <cfset className = getClassName() />
<cfset pkName = getTransfer().getTransferMetadata(className).getPrimaryKey().getName() />
<cfset pkType = getTransfer().getTransferMetadata(className).getPrimaryKey().getType() />
<!--<span class='cc_comment'>-// Determine if this record is a create or an update //---></span>
<cfif getIsPersisted()>
<cfset action = 'Updated' />
<cfelse>
<cfset action = 'Created' />
</cfif>
<!--<span class='cc_comment'>-// Invoke the underlying Transfer save function //---></span> <cfset getTransfer().save(this) />
<!--<span class='cc_comment'>-// Get the primary key value //---></span> <cfset pkValue = this.getID() />
<!--<span class='cc_comment'>-// Construct and save our transaction log record //---></span> <cfset log = getTransfer().new('transactionLog') />
<cfset log.setCompanyID(0) />
<cfset log.setLogItem(className) />
<cfset log.setlogID(pkValue) />
<cfset log.setlogDate(logDate) />
<cfset log.setlogData(arguments.logData) />
<cfset log.setlogType(action) />
<cfset log.setlogIP(cgi.REMOTE_ADDR) />
<!--<span class='cc_comment'>-// If the user is logged in, add to the log //---></span> <cfif isNumeric(user)>
<cfset log.setlogUser(user) />
</cfif>
<cfset getTransfer().save(log) />
</cffunction>
This simply overrides the auto-generated save() function and will first get information about the transfer object to be saved, log it (using transfer still), then invokes the built-in save function. Obviously this has possibilities outside of logging, you could do security checks here, you could add utility / encryption functions here (think encrypting all data in your database, and then decrypting it after reading all in 1 spot)
Anyone else doing something like this?
Getting a web hosting deal is not a problem. The problem is to decide between startlogic or bluehost or the complex netfirms.
Moving all config and XML files out of the web root with Model Glue: Unity
Posted by [Justice] in Coldfusion , Model-Glue , Transfer ORM on October 2, 2007
<br /><cfset ModelGlue_LOCAL_COLDSPRING_PATH = expandPath(<span class='cc_value'>"/auction/config/orbitbid/ColdSpring.xml"</span>) /><br />This allows me to move my ColdSpring.xml file off to a mapped location instead of my web root! You cannot access the mapped locations by browsing to them, but coldfusion still sees them just fine. Inside of ColdSpring.xml, I just modify my configurationPath (and any other necessary paths, including transfer config and generated content folders) to use my mapped location (/mappingName/subFolder/ModelGlue.xml) This just gives me a warm fuzzy that nobody can mess with or view my application configs! For anyone who has not yet tinkered with Model Glue Unity and / or Transfer, you really should take a look. Im already planning on how I can re-code apps I did earlier this year to take advantage of some of the awesome tricks MG:U has up its sleeve.
using Decorators with Transfer ORM
Posted by [Justice] in Coldfusion , Transfer ORM on August 20, 2007
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!
Recent Comments