我是ColdFusion的新手,必须创建一个编辑页面,它将更新和跟踪对表格所做的更改;将登录的AD用户传递给数据库。
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)
到目前为止,我已经构建了页面,并添加了更新Branches表的功能。现在,我正在尝试跟踪更改并记录创建它们的AD用户。
我首先使用SQL Trigger来监视Branches表上的Inserts,Updates,Deletes;但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代码:
<cffunction name="auditBranch" displayname="auditBranch" >
<cfargument name="auditLog" type="query" required="true">
<cfquery name="auditBranchQry" datasource=[...]>
USE [...]
INSERT INTO [dbo].[Audit_BRANCHES]
([BranchID]
,[FieldName]
,[Old]
,[New]
,[ChangeDate]
,[UserID])
VALUES
(
<CFQUERYPARAM CFSQLTYPE="CF_SQL_VARCHAR" VALUE="#auditLog.BranchID#" />
,<CFQUERYPARAM CFSQLTYPE="CF_SQL_VARCHAR" VALUE="#auditLog.FieldName#" />
,<CFQUERYPARAM CFSQLTYPE="CF_SQL_VARCHAR" VALUE="#auditLog.Old#" />
,<CFQUERYPARAM CFSQLTYPE="CF_SQL_VARCHAR" VALUE="#auditLog.New#" />
,getdate()
,<CFQUERYPARAM CFSQLTYPE="CF_SQL_VARCHAR" VALUE="#auditLog.UserID#" />
)
</cfquery>
<cfset testVar = "true">
<cfreturn testVar>
</cffunction>
</cfcomponent>
更新:
以下是来自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);"> </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 :(得分:1)
我在办公室找到了一位同工蜂的错误。
我只需将cfquery包含在cfoutput标记的auditBranchLog中,如下所示:
<cfoutput query="arguments.auditLog">
<cfquery name="auditBranchQry" datasource="[...]">
[...]
</cfquery>
</cfoutput>
这允许ColdFusion循环查询。
我觉得很腼腆。
感谢James的帮助,它确实让我看到了我正在寻找的答案。
答案 1 :(得分: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样式推送查询。
考虑
会很有用<cfdump var="#auditlog#">.
auditBranch
QuerySetCell()
让我觉得可能需要采用不同的方法。<强>更新强>
建议如下
<cfdump var="#auditlog#">
<cfif auditLog.RecordCount NEQ 0>
<cfinvoke component="auditBranchLog" method="auditBranch" auditLog="#auditLog#" >
</cfif>