2017-05-26 82 views
2

我已經閱讀了一些關於Codeigniter交易的內容。我已經在我的代碼中實現了它們,並想知道是否有人可以看一看,看看我是否正朝着正確的方向前進?到目前爲止,我還沒有遇到任何數據庫問題,但我想確保我已經正確地實現了事務。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(); 
    } 

希望有人能幫助我解決這個

+0

集交易使用模型/方法。 – Tpojka

+0

你能告訴我一個例子嗎? – JianYA

回答

0

原因交易是執行多個數據庫查詢操作,如果任何操作失敗撤消任何已經發生。

您只在您的交易塊內執行一項操作,因此交易毫無意義。除此之外,你有這個想法。

在您只使用db->insert()的情況下,您可以輕鬆檢查結果並作出相應的響應。 db->insert()返回true或false。如果返回值爲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"]); 
    } 

} 

以上的交易是有道理的,因爲我們做了兩個分貝OPS如果要麼失敗,我們不想做出的分貝的任何更改。

在控制器模型代碼

if($this->some_model->save_stuff($the_stuff) === FALSE) 
{ 
    redirect('/wherever'); 
} 
//All OK, proceed 
//do other controller things 
+0

謝謝!我需要做一些調整。 – JianYA