2014-04-22 49 views
0

我是ColdFusion的新手,并且必须创建一个编辑页面,以便更新和跟踪对表进行的更改;同时将登录的AD用户传递给数据库。ColdFusion多个cfif条件只返回第一个条件

Environment: 
DB: SQL 2008 R2 
OS: Windows 8 
Server: ColdFusion 8 

Tables Involved: 
Branches (main data) 
Audit_Branches (change tracking data) 

Files involved: 
editBranch.cfm (form) 
updateBranch.cfc (updates Branches table) 
auditBranchLog.cfc (updates Audit_Branches table) 

到目前为止,我已经构建了该页面,并添加了更新分支表的功能。现在,我正试图跟踪更改并记录制作它们的AD用户。

我通过使用SQL触发器来监视分支表上的插入,更新,删除操作;但是SQL Server只跟踪当前的SQL用户。由于该用户是应用程序用户,因此所有用户都具有相同的名称条目。因此,我重新设计了Audit_Branches表,通过CF直接插入数据,因此我可以传递AD用户名。

主要问题:我可以将三个字段中的第一个插入到Audit_Branches表中。如果多个字段发生更改,则只会运行Arguments数组中的第一个字段,而其他字段将被忽略。

我正在使用cf-if语句添加到查询,然后通过cf-invoke将其传递到auditBranchLog函数。

我试过嵌套if,if-else;和/或声明都无济于事。请帮我找出使updateBranch通过一个经过的所有IFS的一个或提出一个更好的方式来一起到auditBranchLog

很大程度上得益于传递信息的最佳方式......

---- -------------- editBranch代码:

<cfquery name="getBranches" datasource=[...]> 

    SELECT 
    BRANCHID, 
    BRANCHDESCRIPTION, 
    BRANCHFILTER, 
    ADDRESS1, 
[...] 
FROM Branches 
where BRANCHID = '#URL.BRANCHID#' 
</cfquery> 

<script> 

function updateBranch(){ 
var branchID = $('#branchID').val() ; 
var branchDescription = $('#branchDescription').val() ; 
var branchDescription_Old = $('#branchDescription_Old').val() ;  
var branchFilter = $('#branchFilter').val(); 
var branchFilter_Old = $('#branchFilter_Old').val() ;  
var address1 = $('#address1').val(); 
var address1_Old = $('#address1_Old').val(); 
[...] 


$.ajax(
    { 
    // the location of the CFC to run 
    url: "updateBranch.cfc" 
    // send a GET HTTP operation 
    , type: "get" 
    // send the data to the CFC 
    , data: { 
    // the method in the CFC to run 
     method: "UpdateBranches" 
    // send the BranchID 
    , branchID: branchID 
    , branchDescription: branchDescription 
    , branchDescription_Old: branchDescription_Old 
    , branchFilter: branchFilter 
    , branchFilter_Old: branchFilter_Old 
    , address1: address1 
    , address1_Old: address1_Old 
[...] 
      } 
    // this gets the data returned on success 
    , success: function doLoad(data) { if (typeof console != "undefined") { 
      console.log(data); 
     }; location.href = "BranchList.cfm"; 

            } 

    }) 
} 



</script> 

</head> 

<body> 

<cfoutput> 

<form class="form-horizontal" action="" method="post"> 

[...] 

<fieldset> 


<!-- Form Name --> 
<legend>Edit Branch</legend> 

<!-- BRANCHID input--> 
<div class="form-group"> 
    <label class="col-md-4 control-label" for="branchID">Branch ID</label> 
    <div class="col-md-4"> 
    <input id="branchID" class="form-control input-md" name="branchID" type="text" placeholder="" 
     value="#getBranches.BRANCHID#" readonly="yes"> 

    </div> 
</div> 

<!-- BRANCHDESCRIPTION input--> 
<div class="form-group"> 
    <label class="col-md-4 control-label" for="branchDescription">Branch Description</label> 
    <div class="col-md-4"> 
    <input id="branchDescription" class="form-control input-md" name="branchDescription" 
     value="#getBranches.BRANCHDESCRIPTION#" type="text" placeholder=""> 

    <input id="branchDescription_Old" name="branchDescription_Old" value="#getBranches.BRANCHDESCRIPTION#" 
     type="hidden"> 


    </div> 
</div> 

<!-- BRANCHFILTER input--> 
<!--- using CFSelect allows to get the db field back for the current selection --->  
<div class="form-group"> 
    <label class="col-md-4 control-label" for="branchFilter">Branch Filter</label> 
    <div class="col-md-4">  
     <select id="branchFilter" class="form-control" name="branchFilter" size="1"> 
     <cfif LEN(getBranches.branchFilter) GT 0> 
      <option value="#getBranches.branchFilter#" selected>#getBranches.branchFilter# (current value)</option> 
      <option value="#getBranches.branchFilter#">---</option>    
     </cfif> 
     <option value="Branch">Branch</option> 
     <option value="Headquarters">Headquarters</option> 
     <option value="RDC">RDC</option> 
     <option value="Automation">Automation</option> 
     </select> 

     <input id="branchFilter_Old" name="branchFilter_Old" value="#getBranches.BRANCHFILTER#" 
     type="hidden"> 


    </div> 
</div> 


<!-- ADDRESS1 input--> 
<div class="form-group"> 
    <label class="col-md-4 control-label" for="address1">Address 1</label> 
    <div class="col-md-4"> 
    <input id="address1" class="form-control input-md" name="address1" type="text" 
     value="#getBranches.ADDRESS1#" placeholder=""> 


    <input id="address1_Old" name="address1_Old" value="#getBranches.ADDRESS1#" 
     type="hidden"> 


    </div> 
</div> 

[...] 

<!-- SUBMIT Button --> 
<div class="form-group"> 
    <label class="col-md-4 control-label" for="SUBMIT"></label> 
    <div class="col-md-4"> 
    <button type = "button" id="SUBMIT" class="btn btn-primary" name="SUBMIT" onClick="updateBranch()">Submit</button> 

    </div> 
</div> 

------------------ updateBranch代码:

<cfcomponent displayname="Branches" access="remote" hint="Update Branches"> 

<cffunction name="getUserID" displayname="getUserID" hint="I pass back the user information in a clean format" access="package" output="false"> 
     <cfset var UserID = ""> 

     <cfset UserID = [...] > 

     <cfreturn UserID> 
</cffunction>  



<cffunction name="UpdateBranches" access="remote" returntype="string" 
    hint="Changes the Branch Info" > 

<cfargument name="branchID" /> 

<cfargument name="branchDescription" /> 
<cfargument name="branchDescription_Old" /> 

<cfargument name="branchFilter" /> 
<cfargument name="branchFilter_Old" /> 

<cfargument name="address1" /> 
<cfargument name="address1_Old" /> 

    <cfset auditLog = QueryNew("BranchID,FieldName,Old,New,UserID", "VarChar, VarChar, VarChar, VarChar, VarChar")>  


<!--- compare old a new and call auditBranchLog.cfc ---> 

<!---BranchDescription---> 
<cfif "#ARGUMENTS.branchDescription#" NEQ "#ARGUMENTS.branchDescription_Old#"> 
    <cfset newrow = QueryAddRow(auditLog)> 
    <cfset temp = QuerySetCell(auditLog,"BranchID","#ARGUMENTS.branchID#")> 
    <cfset temp = QuerySetCell(auditLog,"FieldName","BranchDescription")> 
    <cfset temp = QuerySetCell(auditLog,"Old","#ARGUMENTS.branchDescription_Old#")> 
    <cfset temp = QuerySetCell(auditLog,"New","#ARGUMENTS.branchDescription#")> 
    <cfset temp = QuerySetCell(auditLog,"UserID","#getUserID()#")> 
</cfif> 

<!---BranchFilter---> 
<cfif "#ARGUMENTS.branchFilter#" NEQ "#ARGUMENTS.branchFilter_Old#"> 
    <cfset newrow = QueryAddRow(auditLog)> 
    <cfset temp = QuerySetCell(auditLog,"BranchID","#ARGUMENTS.branchID#")> 
    <cfset temp = QuerySetCell(auditLog,"FieldName","BranchFilter")> 
    <cfset temp = QuerySetCell(auditLog,"Old","#ARGUMENTS.branchFilter_Old#")> 
    <cfset temp = QuerySetCell(auditLog,"New","#ARGUMENTS.branchFilter#")> 
    <cfset temp = QuerySetCell(auditLog,"UserID","#getUserID()#")> 
</cfif> 

<!---Address1---> 
<cfif "#ARGUMENTS.address1#" NEQ "#ARGUMENTS.address1_Old#"> 
    <cfset newrow = QueryAddRow(auditLog)> 
    <cfset temp = QuerySetCell(auditLog,"BranchID","#ARGUMENTS.branchID#")> 
    <cfset temp = QuerySetCell(auditLog,"FieldName","Address1")> 
    <cfset temp = QuerySetCell(auditLog,"Old","#ARGUMENTS.address1_Old#")> 
    <cfset temp = QuerySetCell(auditLog,"New","#ARGUMENTS.address1#")> 
    <cfset temp = QuerySetCell(auditLog,"UserID","#getUserID()#")>   
</cfif> 


<cfif auditLog.RecordCount NEQ 0> 
    <cfinvoke component="auditBranchLog" method="auditBranch" auditLog="#auditLog#" > 
</cfif> 

<cfquery name=[...] 
    </cfquery> 


    <cfreturn arguments.branchID /> 


</cffunction> 
</cfcomponent> 

------------------ auditBranchLog代码:

UPDATE:

下面是从cfdump的信息,我改变了所有三个值:

<th class="query" colspan="6" onClick="cfdump_toggleTable(this);" style="cursor:pointer;" title="click to collapse">query</th> 
</tr> 

<tr bgcolor="eeaaaa" > 
<td class="query" style="cursor:pointer;" title="click to collapse" onClick="cfdump_toggleRow_qry(this);">&nbsp;</td> 

<td class="query">BRANCHID</td> 

<td class="query">FIELDNAME</td> 

<td class="query">NEW</td> 

<td class="query">OLD</td> 

<td class="query">USERID</td> 

</tr> 


<tr > 
<td style="cursor:pointer;" title="click to collapse" onClick="cfdump_toggleRow_qry(this);" class="query">1</td> 


    <td valign="top">YYZ </td> 

    <td valign="top">BranchDescription </td> 

    <td valign="top">BranchLabel-New </td> 

    <td valign="top">BranchLabel </td> 

    <td valign="top">user.name </td> 

</tr> 

<tr > 
<td style="cursor:pointer;" title="click to collapse" onClick="cfdump_toggleRow_qry(this);" class="query">2</td> 


    <td valign="top">YYZ </td> 

    <td valign="top">BranchFilter </td> 

    <td valign="top">Branch </td> 

    <td valign="top">Headquarters </td> 

    <td valign="top">user.name </td> 

</tr> 

<tr > 
<td style="cursor:pointer;" title="click to collapse" onClick="cfdump_toggleRow_qry(this);" class="query">3</td> 


    <td valign="top">YYZ </td> 

    <td valign="top">Address1 </td> 

    <td valign="top">Address1-new </td> 

    <td valign="top">Address1 </td> 

    <td valign="top">user.name </td> 

</tr> 

</table> 
<wddxPacket version='1.0'><header/><data><string>YYZ</string></data></wddxPacket> 
+0

'' - >''更妙的是:'“IS NOT”表示这是一个字符串比较; 'NEQ'表示数字比较。应该注意的是,这是为了让语言学家和其他开发人员知道变量类型,因为操作符执行相同的功能。在任何情况下,哈希标记和引号都不是必需的。 –

回答

1

我发现了什么我是从一位同事蜜蜂在这里的办公室做错了。

我只是需要来包装在auditBranchLog的CFQUERY在CFOUTPUT标签像这样:

<cfoutput query="arguments.auditLog"> 

<cfquery name="auditBranchQry" datasource="[...]"> 
[...] 
</cfquery> 

</cfoutput> 

这使ColdFusion的遍历查询。

我觉得好笑。

谢谢詹姆斯的帮助,它确实让我看到了我正在寻找的答案之外。

+0

如果您不输出任何内容,您可以使用cfloop标签并将查询属性传递给循环。 – osekmedia

0

在此代码

<!---BranchDescription---> 
<cfif "#ARGUMENTS.branchDescription#" NEQ "#ARGUMENTS.branchDescription_Old#"> 
<cfset newrow = QueryAddRow(auditLog)> 
<cfset temp = QuerySetCell(auditLog,"BranchID","#ARGUMENTS.branchID#")> 
<cfset temp = QuerySetCell(auditLog,"FieldName","BranchDescription")> 
<cfset temp = QuerySetCell(auditLog,"Old","#ARGUMENTS.branchDescription_Old#")> 
<cfset temp = QuerySetCell(auditLog,"New","#ARGUMENTS.branchDescription#")> 
<cfset temp = QuerySetCell(auditLog,"UserID","#getUserID()#")> 
</cfif> 


<!---Address1---> 
<cfif "#ARGUMENTS.address1#" NEQ "#ARGUMENTS.address1_Old#"> 
<cfset newrow = QueryAddRow(auditLog)> 
<cfset temp = QuerySetCell(auditLog,"BranchID","#ARGUMENTS.branchID#")> 
<cfset temp = QuerySetCell(auditLog,"FieldName","Address1")> 
<cfset temp = QuerySetCell(auditLog,"Old","#ARGUMENTS.address1_Old#")> 
<cfset temp = QuerySetCell(auditLog,"New","#ARGUMENTS.address1#")> 
<cfset temp = QuerySetCell(auditLog,"UserID","#getUserID()#")>   
</cfif> 


<cfif auditLog.RecordCount NEQ 0> 
    <cfinvoke component="auditBranchLog" method="auditBranch" auditLog="#auditLog#" > 
</cfif> 

看起来你是在某种ORM风格的推查询。

这将是有益的考虑

  1. <cfdump var="#auditlog#">.
  2. 有一个功能,每个行动,而不是一个单一的auditBranch
  3. 看到所有这些QuerySetCell()让我觉得可能需要不同的方法。

UPDATE

以下建议

<cfdump var="#auditlog#"> 


<cfif auditLog.RecordCount NEQ 0> 
    <cfinvoke component="auditBranchLog" method="auditBranch" auditLog="#auditLog#" > 
</cfif> 
+0

我使用 \t 但是,我没有收到任何信息。如何通常显示来自cfc的信息? – ASheppardWork

+0

我没有使用''。通常,当我创建一个对象时,我会为整个请求保留它。所以在我创建一个对象之后,我会做类似'objAudit.auditBranch(auditLog);' –

+0

我找到了答案(见上面),但由于声誉太低而无法发布回答。谢谢詹姆斯的帮助,它让我看到了我寻求的答案之外。 – ASheppardWork