Extending DataMgr 2.5

One of the new features in the upcoming DataMgr 2.5 release that I am really excited about is the ability to extend DataMgr to add or modify its functionality to your liking. This is a feature that John Farrar really had to push me into. Now that I finally see the light, however, I am really excited about the possibilities.

One possibility (also from John) is to return records as arrays of structures instead of queries (for AJAX and the like). I will likely add that into a future build for release in 2.5, but in the meantime it can be added as an extended feature in any copy of the current build.

To do this, I started by opening up DataMgr.cfc. By default, it looks like this:

<cfcomponent extends="_DataMgr">
</cfcomponent>

Since I know that I am going to want to convert a query into an array of structures, I next looked for a function to do that. I found Ben Nadel's QueryToArray which looked to handle my need nicely. I reformatted it very slightly and changed the "access" to "private" and dropped it into DataMgr.cfc.

After that, I just needed to write the actual "getCollections" function. I decided to have the argument tags in the function so that it could be called just like getRecords (either ordinally or by named arguments). The function calls getRecords and return the converted results.

<cffunction name="getCollections" access="public" returntype="any">
   <cfargument name="tablename" type="string" required="yes">
   <cfargument name="data" type="struct" required="no">
   <cfargument name="orderBy" type="string" default="">
   <cfargument name="maxrows" type="numeric" required="no">
   <cfargument name="fieldlist" type="string" default="">
   <cfargument name="advsql" type="struct">
   <cfargument name="filters" type="array">
   
   <cfreturn QueryToArray(getRecords(argumentCollection=arguments))>
</cffunction>

That's it! Now my local copy of DataMgr has a getCollections method that does what I want. That functionality may end up being in the final release of DataMgr 2.5, but the real point is how easy it will be to extend DataMgr to fit your needs.

As of this writing, DataMgr 2.5 is in alpha but the full release is available from RIAForge.org

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Also... when there are updates you can update everything but your extended component. This makes updates better than
copy paste and hope you did it right when base libraries you modify are updated. It's easier to extend, and easier to
keep in sync! :)
# Posted By John Farrar | 9/3/09 11:58 AM
Exactly! Thanks for bringing that up.
# Posted By Steve Bryant | 9/4/09 9:27 AM
That's a really neat idea! I can see that being really useful.
# Posted By John Whish | 9/11/09 3:31 PM
Please elaborate a bit more on the benefits ("usefulness") of returning an array of structures!!? Returning native queries is part of the reason DataMgr has been so clean and nice. I do like the idea of being able to inherit from DataMgr, but moving away from native query objects (beans if you will) just makes it like every other ORM out there (well similar). Is DataMgr headed towards object generations and relations as well?

any thoughts?
# Posted By sal | 2/15/10 3:17 PM
Sal,

The point of this is definitely more about the ability to extend DataMgr to your own liking rather than the specific example of returning an array of structures.

Although I may offer this method as a native option in DataMgr, returning queries is still the focus. For me, queries are one of the under-appreciated gems of ColdFusion.
# Posted By Steve Bryant | 2/15/10 3:29 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.