Skip to main content
June 26, 2007
Answered

CFFUNCTION implementation

  • June 26, 2007
  • 1 reply
  • 331 views
For a project I am currently working on, I inherited a MySQL database which is full of fields of the data type ENUM or SET. This is not how I have normally structured my dbs, so I was struggling with a way to extract the ENUM/SET values in order to present them as SELECT lists in cfforms.

I managed to successfully complete this task, and in an effort to reuse the code across the entire application, have been working on converting the ENUM extraction into a cffunction (code below):

<cffunction name="enumSelect" returnType="query">
<cfargument name="table" type="string" required="true">
<cfargument name="column" type="string" required="true">

<cfquery name = "getEnumValues" dataSource = "#myDB#">
SHOW COLUMNS FROM #table# LIKE '#column#'
</cfquery>

<cfset temp = ValueList(getEnumValues.Type)>
<cfset temp = ReplaceList(#temp#, "enum(,),'", "")>
<cfset queryArray = listToArray(temp)>

<cfset myEnumValues = queryNew("selectOption")>
<cfloop from="1" to="#ArrayLen(queryArray)#" index="x">
<cfset queryAddRow(myEnumValues)>
<cfset querySetCell(myEnumValues, "selectOption", queryArray)>
</cfloop>
<cfreturn myEnumValues>
</cffunction>

Now, my question is, how do I implement this in a real world cfm application? I know the function is working, because if I change the following lines:
<cffunction name="enumSelect" returnType="string"> <!--- changed returnType from "query" to "string" --->
and
<cfreturn myEnumValues.selectOption[2]><!--- added .selectOption[2] --->

and then call the function:
<cfoutput>#enumSelect("table_name","column_name")#</cfoutput>

I successfully print to screen the 2nd option in the ENUM data field. Obviously, if I change the [2] to a [3] I get the third option, etc.

However, I need to implement this function in a CFSELECT. I just don't have an idea how to call it. When I change the function return type back to query, and eliminate the .selectOption[ ] from the cfreturn, and try:

<cfoutput query=#enumSelect("tbgmax_organization_details","Have_Own_Newsletter")#>#selectOption#</cfoutput>

I get the COMPLEX OBJECT TYPES CANNOT BE CONVERTED TO SIMPLE VALUES error.

What am I missing?
    This topic has been closed for replies.
    Correct answer
    Got this working myself. Thought I'd post the solution for public reference, since it's not documented anywhere else that I've seen, specifically as pertains to extracting values from an ENUM or SET data type in MySQL.

    Here's the CFFUNCTION:

    <cffunction name="enumSelect" returnType="query">
    <cfargument name="table" type="string" required="true">
    <cfargument name="column" type="string" required="true">

    <cfquery name = "getEnumValues" dataSource = "#myDB#">
    SHOW COLUMNS FROM #table# LIKE '#column#'
    </cfquery>

    <cfset temp = ValueList(getEnumValues.Type)>
    <cfset temp = ReplaceList(#temp#, "enum(,set(,),'", "")>
    <cfset queryArray = listToArray(temp)>

    <cfset myEnumValues = queryNew("selectOption")>
    <cfloop from="1" to="#ArrayLen(queryArray)#" index="x">
    <cfset queryAddRow(myEnumValues)>
    <cfset querySetCell(myEnumValues, "selectOption", queryArray)>
    </cfloop>
    <cfreturn myEnumValues>
    </cffunction>

    And here's how to deploy it in a cfform as a cfselect list:

    <cfformgroup type="horizontal">
    <CFSET thisList="#enumSelect("Your_Table_Name","Your_Column_Name")#">
    <cfselect
    name = "selectOptions"
    label = "From Function"
    width = "300"
    size = "1"
    multiple="no"
    required = "Yes"
    message = "Select one"
    query = "thisList"
    display ="selectOption"
    value ="selectOption"
    queryPosition="Below">
    </cfselect>
    </cfformgroup>

    It took me a while to figure out the "CFSET thisList" part. Somehow, that makes the cffunction cfreturn accessible to the cfselect, eliminating the COMPLEX OBJECT TYPE error.

    On a final note: I really love ColdFusion, but I have to say, the developer community is very lame in terms of assisting fellow developers. VERY LAME. When I work in PHP, I post a question on any one of a number of forums, and ALWAYS get replies within hours. I have had a great response in this forum from cf_menace on one post -- super thorough and spot-on. But I've seen a few of my posts (and I haven't alway been posting under this account) languish forever. And simple stuff in the end. Stuff any halfway decent developer could have answered in 1 minute. Me being a novice, the solutions took days. Very frustrating.

    1 reply

    Correct answer
    June 26, 2007
    Got this working myself. Thought I'd post the solution for public reference, since it's not documented anywhere else that I've seen, specifically as pertains to extracting values from an ENUM or SET data type in MySQL.

    Here's the CFFUNCTION:

    <cffunction name="enumSelect" returnType="query">
    <cfargument name="table" type="string" required="true">
    <cfargument name="column" type="string" required="true">

    <cfquery name = "getEnumValues" dataSource = "#myDB#">
    SHOW COLUMNS FROM #table# LIKE '#column#'
    </cfquery>

    <cfset temp = ValueList(getEnumValues.Type)>
    <cfset temp = ReplaceList(#temp#, "enum(,set(,),'", "")>
    <cfset queryArray = listToArray(temp)>

    <cfset myEnumValues = queryNew("selectOption")>
    <cfloop from="1" to="#ArrayLen(queryArray)#" index="x">
    <cfset queryAddRow(myEnumValues)>
    <cfset querySetCell(myEnumValues, "selectOption", queryArray)>
    </cfloop>
    <cfreturn myEnumValues>
    </cffunction>

    And here's how to deploy it in a cfform as a cfselect list:

    <cfformgroup type="horizontal">
    <CFSET thisList="#enumSelect("Your_Table_Name","Your_Column_Name")#">
    <cfselect
    name = "selectOptions"
    label = "From Function"
    width = "300"
    size = "1"
    multiple="no"
    required = "Yes"
    message = "Select one"
    query = "thisList"
    display ="selectOption"
    value ="selectOption"
    queryPosition="Below">
    </cfselect>
    </cfformgroup>

    It took me a while to figure out the "CFSET thisList" part. Somehow, that makes the cffunction cfreturn accessible to the cfselect, eliminating the COMPLEX OBJECT TYPE error.

    On a final note: I really love ColdFusion, but I have to say, the developer community is very lame in terms of assisting fellow developers. VERY LAME. When I work in PHP, I post a question on any one of a number of forums, and ALWAYS get replies within hours. I have had a great response in this forum from cf_menace on one post -- super thorough and spot-on. But I've seen a few of my posts (and I haven't alway been posting under this account) languish forever. And simple stuff in the end. Stuff any halfway decent developer could have answered in 1 minute. Me being a novice, the solutions took days. Very frustrating.