Skip to main content
Inspiring
October 31, 2013
Answered

Component properties don't exist anymore.

  • October 31, 2013
  • 1 reply
  • 1926 views

I have an component called: Event.cfc

Long story short, I init() it by issuing a command like:

<cfset eventObject = new 'com.Event'() />

This component has 2 properties: data (a structure) and name (a string).

The init() method calls a private method:

<cfset VARIABLES.setName( VARIABLES.getAttribute( 'event' ) ) />

The setName() method is implicit, ColdFusion builds it based on the name attribute.  I manually created the getAttribute() method which works with the data property.  In the getAttribute() method, I have a command that reads:

<cfset LOCAL.retVar = VARIABLES.data[ ARGUMENTS.attributeName ] />

Well, CF keeps saying that 'data' does not exist in VARIABLES.  But I thought that properties that were defined in a component were put into the VARIABLES scope.  So how can this variable NOT exist when I have it defined at the top of the Event.cfc component as a <cfproperty>?

    This topic has been closed for replies.
    Correct answer Carl Von Stetten

    Aegis,


    What version of CF are you using?

    -Carl V.


    Aegis,

    From the new CF10 docs it looks like <cfproperty> doesn't quite work like you thought:


    Usage

    You must position cfproperty tags at the beginning of a component, above executable code and function definitions. If a component is not used as a web service,  only provides metadata information of the property. It does not define variables or set values that you can use in your component. However, it creates implicit setters and getters for the property in the CFC depending on whether getter/setter attributes are enabled.

    Notice the part I italicized - it does not define the variables.  You'll need to add a pseudo-constructor to create the variables before the init() method.

    Not necessarily related, but something I noticed.  In your init() method you have this:

    <cfset VARIABLES.setName( VARIABLES.getAttribute( 'event' ) ) />

    On a newly created object, the data variable will be empty so calling your getAttribute() method won't return anything (and may error since there won't be an 'event' struct key inside the 'data' variable).

    Also, on methods inside your component, you don't need to call them with the "VARIABLES." scope prefix.

    -Carl V.

    1 reply

    Carl Von Stetten
    Legend
    October 31, 2013

    Can you post the entire actual code of the Event.cfc component?

    -Carl V.

    Inspiring
    October 31, 2013

    <!---

              <!--- ******************************************************************************************************************* --->

              <!--- ******************************************************************************************************************* --->

              Name:                     Event object component.

              File:                              Event.cfc

              Desc:                              Handles View-initiated Event objects.

              Version:          1.0.0 (2013-10-10T12:00:00-0500)

              Todo:                              [none]

              <!--- ******************************************************************************************************************* --->

              <!--- ******************************************************************************************************************* --->

    --->

    <cfcomponent

              output                     = "false"

              accessors          = "true">

              <!--- =================================================================================================================== --->

              <!--- =================================================================================================================== --->

              <!--- Component methods (explicit):

                                            :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

                                            Component methods (implicit):

                                            :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

                                            Component properties:

                                            :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

                                            Private variables:

                                            :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

                                            Public variables:

                                            --->

              <!--- =================================================================================================================== --->

              <!--- =================================================================================================================== --->

              <!--- Component properties: --->

              <cfproperty

                        required          = "true"

                        type                              = "struct"

                        name                              = "data"

                        getter                    = "false"

                        setter                    = "false" />

              <cfproperty

                        required          = "true"

                        type                              = "string"

                        name                              = "name" />

              <!--- =================================================================================================================== --->

              <!--- =================================================================================================================== --->

              <!---          Method: init(). --->

              <cffunction

                        access                              = "public"

                        returntype          = "Event"

                        name                                        = "init"

                        output                              = "false">

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- Arguments. --->

                        <cfargument

                                  required          = "true"

                                  type                              = "struct"

                                  name                              = "constructorData"

                                  default                    = "#{ 'event' = '' }#" />

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- Set LOCAL variables: --->

                        <cfset LOCAL.attributeName = '' />

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- Set instance variables: --->

                        <cfset VARIABLES.setName( VARIABLES.getAttribute( 'event' ) ) />

                        <cfif structCount( ARGUMENTS.constructorData )>

                                  <cfloop collection="#ARGUMENTS.constructorData#" item="LOCAL.attributeName">

                                            <cfset VARIABLES.setAttribute( LOCAL.attributeName, ARGUMENTS.constructorData[ LOCAL.attributeName ] ) />

                                  </cfloop>

                        </cfif>

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!---          Return.          --->

                        <cfreturn THIS />

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

              </cffunction>

              <!--- =================================================================================================================== --->

              <!--- =================================================================================================================== --->

              <!---          Method: getAttribute(). --->

              <cffunction

                        access                              = "private"

                        returntype          = "any"

                        name                                        = "getAttribute"

                        output                              = "false">

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- Arguments. --->

                        <cfargument

                                  required          = "true"

                                  type                              = "string"

                                  name                              = "attributeName" />

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- Set LOCAL variables: --->

                        <cfset LOCAL.retVar = '' />

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- Capture the attribute value: --->

                        <cfset LOCAL.retVar = VARIABLES.data[ ARGUMENTS.attributeName ] />

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!---          Return.          --->

                        <cfreturn LOCAL.retVar />

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

              </cffunction>

              <!--- =================================================================================================================== --->

              <!--- =================================================================================================================== --->

              <!---          Method: setAttribute(). --->

              <cffunction

                        access                              = "private"

                        returntype          = "void"

                        name                                        = "setAttribute"

                        output                              = "false">

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- Arguments. --->

                        <cfargument

                                  required          = "true"

                                  type                              = "string"

                                  name                              = "attributeName" />

                        <cfargument

                                  required          = "true"

                                  type                              = "any"

                                  name                              = "attributeValue" />

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- Store the attribute value: --->

                        <cfset structInsert( VARIABLES.data, ARGUMENTS.attributeName, ARGUMENTS.attributeValue, true ) />

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!---          Return.          --->

                        <cfreturn />

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

                        <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->

              </cffunction>

              <!--- =================================================================================================================== --->

              <!--- =================================================================================================================== --->

    </cfcomponent>

    Carl Von Stetten
    Legend
    October 31, 2013

    I'm chewing through the code.  Next time, please don't post all that whitespace (it looks like a Ben Nadel blog post sample).   Just as a suggestion, you might want to take a look at the ColdBox CFML Standards and Best Practices guide as a way to make code a bit more readable for yourself and others.  I found it to be pretty good.

    Anyway, I'll get back to you in a bit if I can figure something out.

    -Carl V.