繁体   English   中英

Codeigniter如何使用交易

[英]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.

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