Jared Rypka-Hauer, Lead ColdFusion Developer, Minneapolis, MN

Proud Parents of SQLSurveyor and PayPalMX
Viewing By Entry / Main
July 17, 2006 - back to top
OK, just let me say WOW.

I've been struggling with a need to turn a string into a reference to any of the CFMX built-in structs, and in an exchange with Ashwin Matthew last week I found out about a CF built-in function that blew my doors off.

One I'd never heard of.

structGet() is the getter brother of isDefined(). It will search any and all structs available from the root leve of it's scope and that elusive handle. I'd been trying all manner of getPageContext() methods and then I find out I just need to read the doco for CF one last time. ;) It'll even create variables, including array elements, to satisfy your request...

So this will work for things like:

<cfset variables.test = structNew()>
<cfset variables.test.doodle = structNew()>
<cfset variables.test.doodle.foo = "bar">
<cfset myStruct = structGet("doodle")>
<!--- it will even create variables to return, so they're provided just by asking for them --->
<cfset myCreatedStruct = structGet("variables.bar.foo[1].poodle[9]")>


And (and this is the important one):

<cfset sess = structGet("session")>
<cfset frm = structGet("form")>
<cfset cgeye = structGet("cgi")>
<!--- ... and so on for the built-in scopes... --->


And return the handle to the current scope for the currently requesting user!

One major gotcha, however, is illustrated by this snippet:

<cfset test = structNew()>
<cfset test.poo = "test one">
<cfset test.foo = structNew()>
<cfdump var="#variables#" />

<cfdump var="#structGet('test.poo')#">
<cfdump var="#structGet('test.foo')#">
<cfdump var="#variables#" />

If you structGet() a key that's occupied by a simple value, structGet() will blow away the value in the key and replace it with an empty struct... so don't be careless and always use isSimpleValue() or something in your calls to make sure that you're not going to kill something important!

NICE! Thanks for the pointer, Ashwin!

Laterz...

Comments

There are no comments for this entry.




Aura skin for Raymond Camden's BlogCFC provided by Joe Rinehart.