简体   繁体   中英

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. 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?

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

(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

Also as u are using codeigniter, and this applies if u create a junction table, u can use

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM