[英]How to store multiple value in a field which has relationship with other table
I am working on a 'contact databse' using mysql database and codeigniter. 我正在使用mysql数据库和codeigniter开发“联系数据库”。 Some of my table structure is as below
我的一些表结构如下
Organization -> fld_id
fld_orgname
fld_orgaddress
Department -> fld_id
fld_org_id (has relationship with organization)
fld_deptname
fld_details
Designation -> fld_id
fld_dept_id (has relationship with organization)
fld_desgname
usertable -> fld_id
fld_desg (has relationship with Designation)
fld_dept (has relationship with Department)
fld_org (has relationship with organization)
Now the problem is, sometime a person may work in multiple organization. 现在的问题是,有时某个人可能在多个组织中工作。 Then how could i insert this multiple value sequentially to usertable?
那我该如何依次将这个多个值插入到usertable中呢?
One answer is to create another cross reference table for storing the connection between a user and an organization. 一个答案是创建另一个交叉引用表,用于存储用户和组织之间的连接。 That way there can easily be a one to many relationship between a person and organizations
这样一来,人与组织之间就可以轻松地建立一对多关系
user_org => fld_id
user (usertable.fldid)
org (Organization.fld_id)
You should then remove fld_org (has relationship with organization) from usertable 然后,您应该从usertable中删除fld_org(与组织有关系)
(Sorry about my english if im not clear) (抱歉,如果我不清楚我的英语)
If i'm understanding this correctly a user can work in more than one organization and an organization can have multiple users, uno solution may be to create a junction table 如果我正确地理解了这一点,那么用户可以在多个组织中工作,并且一个组织可以有多个用户,则解决方案可能是创建联结表
JUNCTION_ORG_USER -> fld_id_user (usertable) REFERENCES usertable(fld_id)
-> fld_id_org (organization) REFERENCES organization(fld_id)
Or if u dont want (or can't) to normalize, u can repeat the row changing the organization id 或者,如果您不希望(或无法)进行标准化,则可以重复更改组织ID的行
Also as u are using codeigniter, and this applies if u create a junction table, u can use 同样由于您正在使用codeigniter,并且如果您创建联结表,则也适用,您可以使用
$this->db->insert_batch('table_name', $array);
Just build the array like the documentation indicated 像指示的文档一样构建数组
$data = array(
array(
'fld_id' => 'TheUserId',
'fld_org' => 'OrganizationId_1'
),
array(
'fld_id' => 'TheUserId',
'fld_org' => 'OrganizationId_2'
)
);
$this->db->insert_batch('mytable', $data);
Hope my answer helps u. 希望我的回答对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.