CFLib.org – Common Function Library Project

isORM(item)

Last updated May 30, 2010

author

John Farrar

Version: 1 | Requires: CF9 | Library: DatabaseLib

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:

<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:

/**
 * 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;
    }
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson