views:

949

answers:

2

I have this code in my cfm, which works

<cfif not StructIsEmpty(form)>
 <cfset larray = user.getArray() />
 <cfloop collection="#form#" item="key">
  <cfif left(key,4) eq "UPD_">
   <cfset x = listLast(key,"_") />
   <cfset y = evaluate(0,key) />
   <cfloop index="j" from="1" to="#arrayLen(larray)#">
    <cfif (larray[j][1] eq x) and (larray[j][3] neq y)>
     <cfset larray[j][3] = y />
     <cfif not LSIsNumeric(larray[j][3])>
      <cfset larray[j][3] = "0" />
     </cfif>
     <cfset larray[j][4] = "update" />
    </cfif>
   </cfloop>
  </cfif>
 </cfloop>

 <cfloop collection="#form#" item="key">
  <cfif left(key,4) eq "DEL_">
   <cfset x = listLast(key,"_") />
   <cfloop index="k" from="1" to="#arrayLen(larray)#">
    <cfif larray[k][1] eq x>
     <cfset larray[k][4] = "delete" />
    </cfif>
   </cfloop>
  </cfif>
 </cfloop>

 <cfset user = createObject("component", "cfc.User").init(
    identifier = FormatBaseN(form.id,10),
    array = larray
    ) />
</cfif>

<form name="usform" method="POST">
<cfset array = user.getArray() />
<cfoutput>
<cfloop index="i" from="1" to="#arrayLen(array)#">
<table>
 <tr>
  <td><input type="text" name="upd_#array[i][1]#" maxlength="6" size="6" value="#array[i][3]#" /></td>
  <td><input type="checkbox" name="del_#array[i][1]#" /></td>
 </tr>
</table>
<input type="hidden" name="id" value="#user.getIdentifier()#" />
</cfoutput>
</form>

I have put it into a cfc to seperate my logic and my view and i am trying to make it more generic

<cfcomponent name="ArrayManager" output="false">
 <cffunction name="init" hint="constructor" output="false" returntype="ArrayManager">
  <cfargument name="user" type="User" required="true" hint="User bean" />
  <cfargument name="form" type="Struct" required="true" />
  <cfset variables.instance.array = arguments.user.getArray() />
  <cfset variables.instance.form = arguments.form />
  <cfreturn this />
 </cffunction>

 <cffunction name="update" access="public" output="true" returntype="boolean">
  <cfargument name="structstring" type="String" required="true" />
  <cfargument name="seperator" type="String" required="true" />
  <cfset var x = "0" />
  <cfset var y = "0" />
  <cfloop collection="#variables.instance.form#" item="key">
   <cfif key eq "#arguments.structstring#">
    <cfset x = listLast(key,"#arguments.seperator#") />
    <cfset y = evaluate(0,key) />
    <cfloop index="j" from="1" to="#arrayLen(variables.instance.array)#">
     <cfif (variables.instance.array[j][1] eq x) and (variables.instance.array[j][3] neq y)>
      <cfset variables.instance.array[j][3] = y />
      <cfif not LSIsNumeric(variables.instance.array[j][3])>
       <cfset variables.instance.array[j][3] = "0" />
      </cfif>
  <cfset variables.instance.array[j][4] = "update" />
 </cfif>
</cfloop>
   </cfif>
  </cfloop>
  <cfset arguments.user.init(array = variables.instance.array) />
  <cfreturn true />
 </cffunction>

 <cffunction name="delete" access="public" output="false" returntype="boolean">
  <cfargument name="structstring" type="String" required="true" />
  <cfargument name="seperator" type="String" required="true" />
  <cfset var x = "0" />
   <cfloop collection="#variables.instance.form#" item="key">
    <cfif key eq "#arguments.structstring#">
     <cfset x = listLast(key,"#arguments.seperator#") />
     <cfloop index="k" from="1" to="#arrayLen(variables.instance.array)#">
      <cfif variables.instance.array[k][1] eq x>
       <cfset variables.instance.array[k][4] = "delete" />
      </cfif>
     </cfloop>
    </cfif>
   </cfloop>
  <cfset arguments.user.init(array = variables.instance.array) />
  <cfreturn true />
 </cffunction>
</cfcomponent>

And my new cfm

<cfif not StructIsEmpty(form)>
 <cfset arraymanager = createObject("component","cfc.ArrayManager").init(user,form) />
 <cfset seperator = "_" />
 <cfset structstring = "UPD" />
 <cfset arraymanager.update(structstring,seperator) />
</cfif>
...

It fails, i get this error message

The CFML compiler encountered an unexpected coldfusion.compiler.CompilerInternalException exception. The reason for this was: Unable to complete CFML to Java translation. Occurred at:

. . .

The error occurred in C:\path\to\document\root\cfc\ArrayManager.cfc: line 21
Called from C:\path\to\document\root\cfc\update-emp.cfm: line 66
Called from C:\C:\path\to\document\root\cfc\update-emp.cfm: line 66

19 : <cfif key eq "#arguments.structstring#">
20
:
21 : <cfset y = evaluate(0,key) />
22
:
23 `:

What am i doing wrong or is there a better way to accomplish what i am trying to do (showing database content in a table and updating(Update and Delete) the database content through the same table)

+4  A: 

The error message you've posted indicates that you're misusing the Evaluate function. According to the docs, it works like this:

Evaluates one or more string expressions, dynamically, from left to right. (The results of an evaluation on the left can have meaning in an expression to the right.) Returns the result of evaluating the rightmost expression.

But you've got other problems, too. For starters, you're not duplicating the logic correctly when you move the code into the CFC.

In your working code, you use the conditional:

<cfif left(key,4) eq "UPD_">

But in your CFC you have:

<cfif key eq arguments.structString>

This should be:

<cfif left(key,4) eq arguments.structString>

Next, you aren't using the best syntax for evaluate, and you probably don't want to use it at all. The statement:

y = evaluate(0,key)

can be rewritten as:

y = evaluate(key)

Since the value of key is "UPD_something", this can be rewritten as:

y = [variables|arguments|etc].UPD_Something

(Since you're not explicitly specifying a variable scope, CF will attempt to find the variable in a set of scopes in a certain order; which is why I used the syntax [a|b|...])

You probably don't mean this, you probably want the value from the form. And since the key name is dynamic, you should access it this way (instead of using evaluate):

y = variables.instance.form[key]

I think that may fix it. So, to summarize:

  1. Replace your statement <cfif key eq arguments.structString> with <cfif left(key,4) eq arguments.structString> (And make sure that the value you pass as "structString" includes the underscore!)

  2. Replace your use of evaluate with: y = variables.instance.form[key]

I hope that fixes your problems...

After you get it working, start thinking about your variable names. "Array" is a terrible variable name because it is practically a reserved word in CFML. And using "x" and "y" is not descriptive at all. These types of problems are what made this question hard to answer.

Adam Tuttle
+1 Very nice. I've had something similar ready, but after I became insecure if I fully understood the purpose of the original code, I was hesitant to post it. I guess I'll just post the advisory part.
Tomalak
+2  A: 

I fully agree to Adam Tuttle's post. I've removed the "solution part" part of my answer in favor of his. So here's my two cents regarding the "general part":

Avoiding Evaluate() altogether is the best thing you can do. There is no reason to use it apart from actually evaluating pieces of code (which is another bag of hurt that should be avoided). If that's not what you are doing, then there is no situation that could not be resolved through something more appropriate than Evaluate(), e.g.:

<cfset foo = Evaluate("FORM.#foo#")>

is equal to:

<cfset foo = FORM[foo]>

All of the "conveniency" misuses of Evaluate() can be addressed like this.

A few more tips:

  • Avoid meaningless variable names like "x" or "y" (unless you refer to 2D-coordinates, of course).
  • Don't do "#variable#" - you can simply use variable instead.
  • Use Structs wherever you can when you need to access items with a key, e.g.
    <cfif array[i][1] eq SomeValue> is a lot less elegant than
    <cfif array[i]["FieldName"] eq SomeValue>
  • You don't really need "variables.instance" - every component instance has it's own VARIABLES scope. Whatever you stick in there is "instance-only" by default.
  • No need to pass in the FORM scope to a component. The scope is global, so CFCs can see it anyway.
  • Why do you store database stuff in an extra array instead of using the Query object you got when you retrieved them?
  • No need to self-close ("/>") CFML statements - you are not writing XML (though that's a matter of taste).
Tomalak
I disagree on the bit about passing in the form scope. I consider it part of decoupling the component from the page that uses it. Same goes for application scope, session scope, etc (99% of the time)... Generally, you shouldn't directly access them from a CFC.
Adam Tuttle
I'm also a fan of XML/XHTML-formatted CFML tags, but as you said, it's a stylistic choice to be made by the developer. I find that they make it more obvious when a tag in a deeply nested situation does or does not have children; especially helpful when a peer's IDE messes with your indentation.
Adam Tuttle
Hm, I would personally crucify anyone who messes with my indentation. But that's a matter of taste again. ;-) In any case, I don't really see a reason _not_ to use APPLICATION, SESSION, etc. in a CFC, if the CFC is part of the app (and not part of a general-purpose framework). I just don't like to repeat myself in the cffunction arguments all the time. What's the telling argument against that?
Tomalak
i store the database stuff in a object bean, where i save more than just the array information. the meaningless names where just an example. I agree with Adam, i think passing the form scope to the component makes it clearer that the component is supposed to use it. i see i have much to learn, programming for only 4 weeks now.
mrt181