php - What happens to inserted records when a model function returns before it reaches to rollback or complete? -
i trying test transaction management in codeigniter. below method function in model class. if update_user_company_id method returns false, wrapper method save_user_and_company returns. in case, because method returns before reaches
$this->db->trans_complete();
call, changes made save_company , delete_company_requests methods rolled back. want.
but want learn instead of calling
$this->db->rollback();
i directly return method. approach safe? there possibility may encounter lock or other problem in future?
function save_user_and_company($user, $company_request) { $result[statu] = error; $this->db->trans_start(); $company = $this->companies_dao->save_company($company_request->company_name, $user->id); if (!$company) { $result[message] = company_save_error; return $result; } $company_request_result = $this->company_requests_model->delete_company_requests($company_request->id); if (!$company_request_result) { $result[message] = company_request_delete_error; return $result; } $user_update = $this->users_dao->update_user_company_id($user->id, $company->id); if (!$user_update) { $result[message] = user_update_error; return $result; } $this->db->trans_complete(); $result[statu] = success; $result[message] = successful; return $result; }
thanks in advance!
did try this....
following code returns true
if transaction completes successfully.otherwise rollback
transaction , returns false
.hope lot..
function save_user_and_company($user, $company_request) { $this->db->trans_begin(); //begins transaction //performs transaction operations $company = $this->companies_dao->save_company($company_request->company_name, $user->id); $company_request_result = $this->company_requests_model->delete_company_requests($company_request->id); $user_update = $this->users_dao->update_user_company_id($user->id, $company->id); //check whether transaction fails if ($this->db->trans_status() === false) { $this->db->trans_rollback(); $status = false; } else { $this->db->trans_commit(); $status = true; } return $status; }
Comments
Post a Comment