Null, Entities, and CFDUMP

Earlier today I was working with an ORM entity that threw an error when I tried to persist it. Unfortunately it was a very vague error about an empty string trying to be cast as an integer. I’ve already logged a bug report on this. ColdFusion knows what property is wrong it just doesn’t report it in the error. That being said I still had a problem. My entity had close to 60 properties. That’s a crap load. (Technical term.) I quickly added code to cfdump the entity to the file system and opened it up in my browser and noticed that many values which should have been null were reported as empty strings. I could have sworn cfdump supported nulls so I decided to write up a quick test.

I began with a simple, non-ORM based test.

<cfset s = {name="ray", age=javacast("null","") }> <cfdump var="#s#">

This returned...

Yep - nulls are supported. So I then whipped up a super simple persistent CFC.

component persistent="true" { property name="id" column="personID" fieldtype="id" generator="native"; property name="firstname" ormtype="string"; property name="lastname" ormtype="string"; property name="age" ormtype="int"; }

Then wrote this test:

<cfset foo = entityNew("person", {firstname="bob",lastname=""})> <cfdump var="#foo#">

Check out the difference:

As you can see, there is no difference between lastname and age, even though they are clearly very different. I then wrote up another test.

<cfset foo = entityNew("person", {firstname="bob",lastname=""})> <cfset props = getMetaData(foo).properties> <cfloop index="prop" array="#props#"> <cfif structKeyExists(prop, "name")> <cfoutput>Testing property #prop.name# = <cfinvoke component="#foo#" method="get#prop.name#" returnvariable="res"> <cfif isNull(res)> NULL <cfelseif isSimpleValue(res)> #res# <cfelse> complex </cfif> <br/> </cfoutput> </cfif> </cfloop>

As you can see, I get the metadata and then loop over all of the properties and call the getter. I then look at the response. When I run this I get...

Testing property id = NULL Testing property firstname = bob Testing property lastname = Testing property age = NULL

I ran this on my 50+ property ORM entity and was able to find two properties that had been accidentally set to "" instead of a null value. I've logged a bug for this if you want to vote for it: 86880 I'd imagine this could be fixed in cfdump in about 60 seconds if the code was unencrypted.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments