CFLib.org – Common Function Library Project

isORM(item)

Last updated May 30, 2010

Version: 1 | Requires: ColdFusion 9 | Library: DatabaseLib

 
Rated 0 time(s). Average Rating: 0

Description:
There is no built in way to check this with a single function at this time. So I created a way to check if the element was either and ORM entity or collection of entities.

Return Values:
Returns a boolean.

Example:

view plain print about
<cfscript>
myStruct = structNew();
myQuery = queryNew();
selector = { id = 12 };
myEntity = EntityLoad('entityClass',selector,true);
</cfscript>

<cfoutput>
myStruct : #is_orm(myStruct)#<br>
myQuery : #is_orm(myQuery)#<br>
myEntity: #is_orm(myEntity)#<br>
</cfoutput>
- - - - - 
This will give the following result.
- - - - -
myStruct : false
myQuery : false
myEntity : true

- - - - -

We are using this logic inside our ReturnData.cfc inside COOP. It enables us to treat the entity as a generic data source and integrate lists, array of structures, queries and entities as data sources without having to manually set an attribute on custom tags and object methods. It's nice to have some intuition in our code.

Parameters:

Name Description Required
item Value to check. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Validates if item is orm entity or collection.
 * 
 * @param item      Value to check. (Required)
 * @return Returns a boolean. 
 * @author John Farrar (johnfarrar@sosensible.com) 
 * @version 1, May 30, 2010 
 */

function isORM(item) {
    var metaItem = {};
    try {
        if(isArray(arguments.item) && arrayLen(arguments.item)){
            metaItem = getMetadata(arguments.item[1]);
        } else {
            metaItem = getMetadata(arguments.item);
        }
        if(structKeyExists(metaItem,'persistent') && metaItem.persistent){
            return true;
        } else {
            return false;
        }
    } catch(any e) {
        return false;
    }
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

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

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

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

Ben Forta Ben Forta added
GetHostAddress
22 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