Copy link to clipboard
Copied
Hi:
I am using c++ to write a custom coldfusion tag. There are two ways of returning values, one is to use:
request.setVariable(name, value);
The other one is to use queries:
pRequest->AddQuery(name, columns);
...
I do not understand the difference between them. Can someone make an example of the difference of what they will look like after returned. Or to demonstrate in anyway that I could understand.
Thanks.
Copy link to clipboard
Copied
I am more familar with java CFX tags. But I think the main difference is that they are used to add different types of variables to the calling page. Response.AddQuery is specifically for creating a CF query object. Think of it as the equivalent of using QueryNew() in a cfm page:
<cfset theQueryName = QueryNew("FirstName,LastName")>
Response.SetVariable is used to create other types of variables like strings, datetime objects, structures, etc. (Update In java tags you can tweak the "name" value create structures and arrays.) It is the equivalent of using a cfset such as:
pRequest->
setVariable
("firstName", "John");
...
<!--- create a string --->
<cfset firstName = "John">
<!--- .. or a date/time object --->
<cfset startDate = createDate(2009, 3, 1)>
<!--- .. or a complex object like a structure --->
<cfset prop = structNew()>
Copy link to clipboard
Copied
I checked some documentation other than adobe's. It seems like setVariable() is just like <cfset>, which only takes value of string. In our case is John.
And query is like <cfquery>, which act like you communicate with a SQL server. A SQL server return you a set of data for instance containing a whole table of user information. So we need <cfloop> to fetch out each row, or use <cfoutput> to print out the whole table.
Can anyone confirm this?
Copy link to clipboard
Copied
cpthk0 wrote:
I checked some documentation other than adobe's. It seems like setVariable() is just like <cfset>, which only takes value of string. In our case is John.
Yes, but you can adjust the "name" value to implicitly create other types of objects like structures or arrays.
And query is like <cfquery>, which act like you communicate with a SQL server. A SQL server return you a set of data for instance containing a whole table of user information. So we need <cfloop> to fetch out each row, or use <cfoutput> to print out the whole table.
Are you asking about how cfquery is used within a ColdFusion page or how to populate query objects from a CFX tag?
Copy link to clipboard
Copied
yes, I think you are right. But query act exactly like SQL return query, which is like a table of data. setVariable() you will have to fetch out data yourself, coldfusion will take care query for you.
Copy link to clipboard
Copied
cpthk0 wrote:
yes, I think you are right. But query act exactly like SQL return query, which is like a table of data. setVariable() you will have to fetch out data yourself, coldfusion will take care query for you.
Essentially, yes. But it depends on the object type. There are different kinds of CF loops that simplify extracting data. With CF8 you can loop through arrays and structures almost as easily as queries. So use whichever object type makes the most sense for your tag.