Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I stop using evaluate() function?

I have a table with values under a column named:str_condition

values in this column can be : variables.bit_male / application.bit_male / isdefined('session.int_user_id')

The value can be complex as it can be.

I need to use the value of the values in the column.

Currently, what I am doing is

<cfif evaluate(query.str_condition)  eq true>
.....code....
</cfif>

Now, I need to omit the evaluate.

like image 311
renjithmohanan Avatar asked May 09 '14 12:05

renjithmohanan


2 Answers

TBH, I'd stick with evaluate() for this: you're leveraging one of the few situations it makes sense. Provided what you have in the DB field is just an expression (no tags), then evaluate() will work fine.

As others have suggested... storing expressions in the DB is not ideal, but I can see how sometimes it might be the best approach, However do reconsider it though, in case you can come up with a different approach entirely (this is situation-specific, so we can't really give you guidance on that).

The only other real option for you would be to write the code from the DB to file then include it, but that would be a worse solution than just using evaluate(), I think.

A lot of people get hung up on the dogma that is evaluate() is bad, without really stopping to think about why that's the case... it's unnecessary for most situations people use it, but it's completely fine in situations in which it is needed (such as yours).

like image 134
Adam Cameron Avatar answered Nov 15 '22 02:11

Adam Cameron


This is an edited answer, since I originally misread the question.

In many cases, array notation is your freind

<cfif queryname['fieldname'][rownumber] is true>
code for true

Note that the queryname is not quoted but the fieldname is. If you don't quote the fieldname, ColdFusion will assume it's a variable.

Also pertinent is that if you are storing things in a database, such as code, that you want to select and then execute, you have to select those things, write them to another .cfm file, and then cfinclude that file. That's somewhat inefficient.

In your case, you are storing variable names in your database. If using evaluate is giving you the correct results, anything you change would likely be a change for the worse.

like image 43
Dan Bracuk Avatar answered Nov 15 '22 03:11

Dan Bracuk