CFLib.org – Common Function Library Project

arrayOfObjectsToQuery(theArray)

Last updated June 11, 2009

Version: 0 | Requires: ColdFusion MX | Library: DataManipulationLib

 
Rated 0 time(s). Average Rating: 0

Description:
Converts an array of objects to a CF Query Object. Only works with "Bean" style CFCs. Uses the result of all getX methods to populate the query. Based on arrayOfStructuresToQuery by David Crawford http://cflib.org/udf/ArrayOfStructuresToQuery

Return Values:
Returns a query.

Example:

view plain print about
<cfset test1 = CreateObject('component','someObj').init( 'foo', 'bar' ) />
<cfset test2 = CreateObject('component','someObj').init( 'foo1', 'bar1' ) />
<cfset myArray = ArrayNew(1) />
<cfset arrayAppend(myArray,test1) />
<cfset arrayAppend(myArray,test2) />

<cfset QoQ = arrayOfObjectsToQuery(myArray)>

<cfdump var="#QoQ#" />

Parameters:

Name Description Required
theArray The array of CFCs. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Converts an array of objects to a CF Query Object.
 * 
 * @param theArray      The array of CFCs. (Required)
 * @return Returns a query. 
 * @author Don Quist (don.sigmaprojects@gmail.com) 
 * @version 0, June 11, 2009 
 */

function arrayOfObjectsToQuery(theArray){
    var colNames = ArrayNew(1);
    var theQuery = queryNew("");
    var i=0;
    var j=0;
    var o=0;
    var functions = '';
    //if there's nothing in the array, return the empty query
    if(NOT arrayLen(theArray)) return theQuery;
    
    //get meta data for the first object in the array and set the functions
    functions = getMetaData(theArray[1]).functions;
    //get the column names into an array =    
    for(o=1; o LTE arrayLen(functions); o=o+1){
        ifREFindNoCase( 'get+', functions[o].NAME ) and functions[o].NAME IS NOT 'init' ) {
            arrayAppend(colNames, LCase(REReplaceNoCase(functions[o].NAME, "^get",'' )) );
        }
    }

    theQuery = queryNew(arrayToList(colNames));
    
    //add the right number of rows to the query
    queryAddRow(theQuery, arrayLen(theArray));
    //for each element in the array, loop through the columns, populating the query
    for(i=1; i LTE arrayLen(theArray); i=i+1){
        for(j=1; j LTE arrayLen(colNames); j=j+1){
            //bug out incase something isnt defined in the object
            try {
                querySetCell(theQuery, colNames[j], Evaluate('theArray[i].get#colNames[j]#()'), i);
            }
            catch(Any excpt) { }
        }
    }
    return theQuery;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
11 day(s) ago

Will Belden Will Belden added
longTime
17 day(s) ago

James Sleeman James Sleeman added
quickSort
27 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
30 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson