![](/img/trans.png)
[英]how to use transactions in codeigniter for commit and rollback based on data change?
[英]Codeigniter how to use transactions
我已經閱讀了有關Codeigniter中事務的一些知識。 我已經在我的代碼中實現了它們,並且想知道是否有人可以看看我是否朝着正確的方向前進? 到目前為止,我還沒有遇到任何數據庫問題,但是我想確保正確實現了事務。
在我的代碼中,我首先創建用戶詳細信息並獲取ID,然后將該ID插入到用戶帳戶表中。
調節器
if($this->form_validation->run()==false){
$this->index();
}else{
$password = md5($password);
$package = array(
'first_name'=>$first_name,
'last_name'=>$last_name,
'email'=>$email,
'client_id'=>$client_id,
'date_of_birth'=>$date_of_birth,
'phone_number'=>$phone_number,
'address'=>$address,
'country'=>$country,
'created_on'=>$this->get_date(),
'updated_on'=>$this->get_date()
);
if($this->AccountModel->user_account_exists($email)){
$this->session->set_flashdata('Error',"Account already exists");
redirect('/Register');
}else{
$this->db->trans_start();
$id = $this->AccountModel->create_person($package);
$error = $this->db->error();
$this->db->trans_complete();
$expiration_date = date('Y-m-d', strtotime($this->get_date() . "+1 month") );
if($this->db->trans_status()===false){
$this->index();
}else{
$account = array(
'username'=>$email,
'password'=>$password,
'user_type'=>'user',
'person_id'=>$id,
'is_active'=>true,
'created_on'=>$this->get_date(),
'updated_on'=>$this->get_date(),
'expires_on'=>$expiration_date,
'status'=>'active'
);
$this->db->trans_start();
$id = $this->AccountModel->create_user_account($account);
$error = $this->db->error();
$this->db->trans_complete();
if($this->db->trans_status()===false){
$this->index();
}else{
$this->session->set_flashdata('Success','Account has been created');
redirect('/Login');
}
}
}
if($error!=''){
$this->session->set_flashdata('Error',$error["message"]);
redirect('/Register');
}
}
模型
public function create_user_account($input){
$this->db->insert('user_accounts',$input);
return $this->db->insert_id();
}
public function create_person($input){
$this->db->insert('person',$input);
return $this->db->insert_id();
}
希望有人可以幫助我
事務的原因是執行多個數據庫查詢操作,如果任何操作失敗,請撤消已經發生的任何操作。
您僅在事務塊內執行一項操作,因此事務毫無意義。 除此之外,您已經有了主意。
在僅使用db->insert()
的情況下,您可以輕松地檢查結果並做出相應的響應。 db->insert()
返回true或false。 如果返回為假,則獲取錯誤並將其設置為flashdata。 否則,繼續您的工作。
正如@Topjka所說,事務應該在模型代碼中。
這是一個樣本模型
class Some_model extends CI_Model
{
public function save_stuff($data)
{
//let's assume $data has values for two different tables
$newInfo = $data['newStuff'];
$updateInfo = $data['oldStuff'];
$this->db->trans_start();
$this->db->insert('other_table', $newInfo);
$this->db->update('one_table', $updateInfo);
$this->db->trans_complete();
if($this->db->trans_status() === FALSE)
{
$this->set_flash_error();
return FALSE;
}
return TRUE; //everything worked
}
public function set_flash_error()
{
$error = $this->db->error();
$this->session->set_flashdata('Error', $error["message"]);
}
}
上面說明了事務的合理性,因為我們執行了兩個數據庫操作,並且如果兩個操作都失敗了,我們不希望對數據庫進行任何更改。
在控制器中使用模型/方法
if($this->some_model->save_stuff($the_stuff) === FALSE)
{
redirect('/wherever');
}
//All OK, proceed
//do other controller things
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.