Storing key-value pairs that have a one-to-many relationship in the SESSION scope

时间:2015-05-24 21:19:42

标签: coldfusion coldfusion-10 coldfusion-11

A user can belong to many Companys which in turn have many Departments.

When a user logs in, I want to store in the SESSION scope which Departments in which Companies he belongs to.

I am currently doing it like this (the getDepartments query contains CompanyName and DepartmentName records that the user belongs to):

<cfset SESSION.Department = StructNew()/>
<cfloop query="getDepartments" group="CompanyName">
    <cfloop>
    <cfset SESSION.Department[getDepartments.CompanyName][getDepartments.DepartmentName] = StructNew()/>
    </cfloop>
</cfloop>

If I run the above I get an output like this:

struct
NESTLE
   struct
   SALES      - struct [empty]
   struct
   HR         - struct [empty]
----------------------------------------------
struct
MARS
   struct
   LOGISTICS   - struct [empty]

So each Department within a Company is being created a struct. And because there's no value to assign to a Department name it shows it as empty.

I think I am doing this incorrectly. But equally, I don't know of another way assign multiple Department names to a single Company name in the SESSION scope.

Any ideas?

1 个答案:

答案 0 :(得分:1)

另一种解决方案可能是为每家公司创建一系列部门!而不是结构。像这样。

<cfset SESSION.Department = StructNew()/>
<cfloop query="getDepartments" group="CompanyName">
    <cfset SESSION.Department[getDepartments.CompanyName]=arrayNew(1)/>
    <cfset i=0>
    <cfloop>
        <cfset i++ />
        <cfset SESSION.Department[getDepartments.CompanyName][i] = getDepartments.DepartmentName/>
    </cfloop>
</cfloop>

enter image description here