Coldfusion从动态表单复选框创建数组

时间:2016-03-02 23:37:21

标签: arrays forms checkbox coldfusion cfloop

我有一个包含多个复选框的动态表单,在提交时,我想仅使用更改状态的复选框值在另一个CFSTOREDPROC中运行cfloop

到目前为止,我正在尝试测试一个粗略的概念,但我确信我创建数组的方式会有问题。如果有人可以就可能的解决方案提供反馈,我将不胜感激。

HTML / CF FORM:

<form action="self.cfm" method="post" name="permissions">
 <input type="hidden" name="User_ID" value="<CFOUTPUT>#User_ID#</CFOUTPUT>">
<table>
<CFLOOP QUERY="getNthPermission">
<tr><td>#getNthPermission.Permission_Name#</td><td><input type="checkbox" value="#getNthPermission.Permission_ID#" name="#getNthPermission.Permission_Name#" <CFIF LISTVALUECOUNT(VARIABLES.UserPermission_ID_List,getNthPermission.Permission_ID) NEQ 0>CHECKED</CFIF>></td></tr>
</CFLOOP>
</table>
<input type="submit" name="submit" value="Update">
</form>

冷冻行动:

<CFSET VARIABLES.Permission_ID_List = ValueList(getUserPermission.Permission_ID)>
    <cfset changed_permissions=ArrayNew()>
    <CFLOOP QUERY="getNthPermission">
    //If it was checked when the form was created but was unchecked by the user add it to my array.
    <CFIF LISTVALUECOUNT(VARIABLES.UserPermission_ID_List,getNthPermission.Permission_ID) NEQ 0  AND !IsDefined(FORM.#getNthPermission.Permission_Name#)>
    <cfset changed_permissions[getNthPermission.Permission_ID]>
    <CFELSEIF LISTVALUECOUNT(VARIABLES.UserPermission_ID_List,getNthPermission.Permission_ID) EQ 0  AND IsDefined(FORM.#getNthPermission.Permission_Name#)>
    //If it wasn't checked when the form was built but was checked by the user add it to my array.
    <cfset changed_permissions[getNthPermission.Permission_ID]>
    </CFIF>
    </CFLOOP>

//现在遍历刚刚创建的数组并将存储过程传递给我的值

<CFLOOP from="1" to="#arrayLen(changed_permissions)#" index="i">
    <CFSTOREDPROC DATASOURCE="#MYDB_DSN#" PROCEDURE="Update_UserPermission">
        <CFPROCPARAM DBVARNAME="@PermissionList" VALUE="#changed_permissions[i]#" TYPE="IN" CFSQLTYPE="cf_sql_longvarchar">
        <CFPROCPARAM DBVARNAME="@User_ID" VALUE="#FORM.User_ID#" CFSQLTYPE="cf_sql_integer">
    </CFSTOREDPROC>
</CFLOOP>

更新

我正在使用我不参与设置的数据库配置,其中包含以下表格:

  • 权限表(permission_name,ID) - 权限列表
  • 用户表(user_name,ID)
  • User_Permissions表(Permissions_ID,User_ID) - 包含用户有权访问的每个权限的条目。

因此,当选中/取消选中复选框时,我要么添加新条目,要么删除一个条目。哪个存储过程已经存在(或者我希望它确实存在)。

1 个答案:

答案 0 :(得分:3)

三个表是一个很好的设置。我担心它是单个表,或者值存储为csv列表{shudder}。仍然无法真实地将您的表单可视化,但是......从您所描述的内容来看,通过一些查询而不是循环来执行此操作会更简单。

首先,给复选框指定相同的名称,如&#34; Permission_IDList&#34;,以便将ID作为列表提交。然后使用以下选项之一使用该ID列表:

选项1:删除/插入所有

我经常使用简单junction tables的方法是首先删除所有现有用户权限。然后使用INSERT / SELECT插入 new 权限。使用INSERT / SELECT的两大优点是它不需要循环,还提供内置验证。

这是最简单的选项之一,但它不如选项2精确,因为它每次都会删除所有记录。从技术上讲,它可能比许多情况下需要的工作多一些。虽然除非您处理大量记录,否则差异通常可以忽略不计。沿着这些方向的东西(未经测试):

    --- First remove all existing permissions for User
    DELETE FROM User_Permissions 
    WHERE  User_ID = <cfqueryparam value="#FORM.User_ID#" cfsqltype="cf_sql_integer">

    --- Now re-insert current/new permissions 
    INSERT INTO User_Permissions  ( User_ID, Permissions_ID )
    SELECT  u.ID AS User_ID
            , p.ID AS Permissions_ID
    FROM    UserTable u CROSS JOIN PermissionTable p   
    WHERE   u.ID = <cfqueryparam value="#FORM.User_ID#" cfsqltype="cf_sql_integer">
    AND     p.ID IN 
           (
                <cfqueryparam value="#FORM.Permission_IDList#" cfsqltype="cf_sql_integer" list="true">
           )

选项2:DELETE已更改/ INSERT已添加

另一种选择是使用查询来识别和删除已删除的权限,即未选中。然后使用INSERT / SELECT插入添加的权限。其余的保持不变。它比选项1稍微复杂一点,但更精确的是它只删除或添加实际更改的内容。再次,没有经过测试,但是这样的事情:

    --- existing id's NOT in the new list were UN-checked
    DELETE FROM User_Permissions 
    WHERE  User_ID = <cfqueryparam value="#FORM.User_ID#" cfsqltype="cf_sql_integer">
    AND    Permissions_ID NOT IN 
           (
                <cfqueryparam value="#FORM.Permission_IDList#" cfsqltype="cf_sql_integer" list="true">
           )


    INSERT INTO UserPermissions ( User_ID, Permissions_ID )
    SELECT u.ID AS User_ID
            , p.ID AS Permissions_ID
    FROM   UserTable u 
                CROSS JOIN PermissionsTable p 
                LEFT JOIN UserPermissions up ON up.User_ID = u.ID AND up.Permissions_ID = p.ID
    --- this user + permission does not already exist
    WHERE  up.Permissions_ID IS NULL        
    AND    u.ID = <cfqueryparam value="#FORM.User_ID#" cfsqltype="cf_sql_integer">
    AND    p.ID IN 
        (
                    <cfqueryparam value="#FORM.Permission_IDList#" cfsqltype="cf_sql_integer" list="true">

        )

NB:确保将两个查询都包装在一个事务中,以便将它们视为一个单元,即两者都成功或两者都失败。