好的,所以我们试图通过具有定义字段的数据库表来变得聪明,例如:
(1) id, name, title, datemodified, dateadded
然后将它们扩展到各种“对象”,例如
联系表
(2) id, name, title, datemodified, dateadded, sitecode, contactid
或者:
文章表
(3) id, name, title, datemodified, dateadded, sitecode, articleid, votes
所以你会注意到 (1) 成为一个基础,而 (2) 和 (3) 扩展。
我们有一个基础对象,它对这些基础字段进行数据库查询,然后我们尝试使用以下代码神奇地扩展它:
<cfquery name="local.qReturnQuery" datasource="#variables.sDSN#">
SELECT id, name, title, datemodified, dateadded, sitecode, contactid
FROM tbl_#arguments.sPrefix# cb
WHERE 1
<cfif arrayLen(arguments.aExtendedParams) gt 0>
<cfloop from="1" to="#arrayLen(arguments.aExtendedParams)#" index="local.x">
<cfscript>
local.sParamField = arguments.aExtendedParams[local.x][1];
local.sParamValue = arguments.aExtendedParams[local.x][2];
local.sParamType = arguments.aExtendedParams[local.x][3];
local.bParamIsList = arguments.aExtendedParams[local.x][4];
local.sParamCondition = arguments.aExtendedParams[local.x][5];
local.bIsPositive = arguments.aExtendedParams[local.x][6];
</cfscript>
<cfswitch expression="#local.sParamType#">
<cfcase value="integer,boolean" delimiters="true">
#local.sParamCondition#
<cfif local.bParamIsList>
#local.sParamField# <cfif not local.bIsPositive>NOT </cfif>IN (<cfqueryparam cfsqltype="cf_sql_integer" value="#local.sParamValue#" list="true">)
<cfelse>
#local.sParamField# <cfif not local.bIsPositive>!</cfif>= <cfqueryparam cfsqltype="cf_sql_integer" value="#local.sParamValue#">
</cfif>
</cfcase>
<cfcase value="string">
#local.sParamCondition#
<cfif local.bParamIsList>
#local.sParamField# <cfif not local.bIsPositive>NOT </cfif>IN (<cfqueryparam cfsqltype="cf_sql_longvarchar" value="#local.sParamValue#" list="true">)
<cfelse>
#local.sParamField# <cfif not local.bIsPositive>!</cfif>= <cfqueryparam cfsqltype="cf_sql_longvarchar" value="#local.sParamValue#">
</cfif>
</cfcase>
</cfswitch>
</cfloop>
</cfif>
</cfquery>
不幸的是,我令人难以置信的代码似乎忽略了我cfswitch
并输出如下查询:
SELECT id, name, title, datemodified, dateadded, sitecode, contactid
FROM tbl_contact_thing cb
WHERE 1
我的数组看起来像:
arguments.aExtendedParams = [{1="contactid",2="44",3="integer",4="false",5="AND",6="true"}];
所以应该看起来像:
SELECT id, name, title, datemodified, dateadded, sitecode, contactid
FROM tbl_contact_thing cb
WHERE 1
AND contactid = 44
我可能做错了什么(就这段代码而言)