Skip to main content
Participant
December 11, 2009
質問

StructGet not Working Correctly for Scalar Variables in CF 9?

  • December 11, 2009
  • 返信数 1.
  • 869 ビュー

In ColdFusion 9, the following code snippet:

     <cfset x = "5">
     <cfset y = StructGet("x")>
     <cfoutput>isStruct(y) = #isStruct(y)#<br></cfoutput>
     <cfoutput>y = <br></cfoutput><cfdump var = "#y#"><cfoutput><br></cfoutput>

returns:

     isStruct(y) = NO
     y =
     5

even though the CF documentation states that "if you accidentally use this function on a variable that is not a structure, it replaces the value with an empty structure" (see http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-6210.html).

Under ColdFusion 8, the above code snippet does set up y as a structure.

Am I doing something wrong in the above code snippet, or is this a bug in ColdFusion 9?

    このトピックへの返信は締め切られました。

    返信数 1

    BKBK
    Community Expert
    Community Expert
    December 11, 2009

    The way I understand it, structget() represents a deeply nested structure as a simpler one. In the following example, the structure z.y.x.w is represented simply as the structure w.

    It implies that w.v is shorthand for z.y.x.w.v. The value of z.y.x.w.v is therefore 5.

    <cfset w = StructGet("z.y.x.w")>
    <cfset w.v = "5">

    <cfoutput>isStruct(w) = #isStruct(w)#</cfoutput><br>
    <cfoutput>isStruct(z.y.x.w) = #isStruct(z.y.x.w)#</cfoutput><br><br>

    z.y.x.w.v = <cfoutput>#z.y.x.w.v#</cfoutput>

    Mitch Kirsch作成者
    Participant
    December 12, 2009

    My question is whether or not StructGet in ColdFusion 9 is working correctly with scalar variables.  That is, my application employs the Spectra content management system, and buried inside of such custom tags as cfa_contentObjectGet and cfa_contentObjectCreate are statements of the form

         <cfset stEmbeddedObject = StructGet("stObject.#scope#")>

    where the scope variable contains the name of one of the properties in the stObject structure.  In ColdFusion 8, no matter what scope referred to, stEmbeddedObject would be a structure; in ColdFusion 9, if scope refers to a scalar variable, stEmbeddedObject ends up being a scalar variable, not a structure (and thus subsequent statements that treat stEmbeddedObject as a structure blow up).

    BKBK
    Community Expert
    Community Expert
    December 12, 2009
    in ColdFusion 9, if scope refers to a scalar variable, stEmbeddedObject
    ends up being a scalar variable, not a structure (and thus subsequent
    statements that treat stEmbeddedObject as a structure blow up).

    Not quite true. When I run the following in Coldfusion 9, it displays Yes

    <cfset scope=  5>
    <cfset stEmbeddedObject = StructGet("stObject.#scope#")>
    <cfoutput>#isStruct(stEmbeddedObject)#</cfoutput>

    However, the documentation on structGet warns you on several occasions that Coldfusion does its best to store the return-variable of the function as a struct. In other words, Coldfusion takes your word that the argument of the function is a valid struct. It is your responsibility to make sure it is.

    For example, even though Coldfusion tells you stEmbeddedObject is a struct, the code will bomb if you tried to use it as such. That is because stObject.5 isn't a valid structure. In my opinion, StructGet's assignment of stEmbeddedObject to a struct happens at compile time. Coldfusion makes no guarantees at runtime.