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;
	}
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson