php - How to get last id for relationships in a transaction between models in Codeigniter? -
i have code inside function in user-controller. i'm, using codeigniter.
$name_user = $this->input->post('contact-company'); $company_name = $this->input->post('name-company'); $company_orgnr = $this->input->post('orgnr-company'); $phone_company = $this->input->post('phone-company'); $email_company = $this->input->post('email-company'); //insert a new user $user_info = array( 'username' => $email_company, 'name_user' => $name_user ); $company_info = array( 'name' => $company_name, 'orgnr' => $company_orgnr, 'phone' => $phone_company, 'email' => $email_company ); //insert new user in db $query_insertuser = "start transaction"; //get sql inserting new user $um = new usermodel(); $query_insertuser .= $um->getsqlinsert($user_info); //get sql inserting new company $cm = new companymodel(); $query_insertuser .= $cm->getsqlinsert($company_info); //get sql inserting relation between user //and company (how id of user , id of company use?) $upm = new userprofilemodel(); $query_insertuser .= $upm->getsqlinsert(); $query_insertuser .= "commit"; //do atual insert $um->insert($query_insertuser);
it's used handling registration through form. (validation done through form-validation libray)-
- users username, name_user stored in users-table
- companies stored in companies-table
- relations between companies , users stored in userprofile-table
i think code kind of self-explainatory, i'm not clear in how insert relation in userprofile-model. need last inserted id user , last inserted id company, in code don't have because don't insert users or companies before calling $upm->getsqlinsert();
or doing incorrectly? please give me pointers...
i not doing correctly. in case care bother solution:-)
user-controller (part of it)
//insert new user in db $um = new usermodel(); $um->starttransaction(); $user_id = $um->insert($user_info); $cm = new companymodel(); $company_id = $cm->insert($company_info); //insert new user-profile newly inserted user $upm = new userprofilemodel(); $userprofile_info = array( 'user_id' => $user_id, 'company_id' => $company_id ); $upm->insert($userprofile_info); $um->endtransaction();
user-model (part of it)
public function starttransaction() { $this->db->trans_start(); } public function endtransaction() { $this->db->trans_complete(); } public function insert(array $user_info) { $this->db->insert('user', $user_info); return $this->db->insert_id(); }
company-model (part of it)
public function insert(array $company_info) { $this->db->insert('company', $company_info); return $this->db->insert_id(); }
userprofile-model (part of it)
public function insert(array $userprofile_info) { $this->db->insert('user_profile', $userprofile_info); return $this->db->insert_id(); }
Comments
Post a Comment