Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter如何使用事务_Php_Mysql_Codeigniter - Fatal编程技术网

Php Codeigniter如何使用事务

Php Codeigniter如何使用事务,php,mysql,codeigniter,Php,Mysql,Codeigniter,我读过一些关于Codeigniter中事务的东西。我已经在我的代码中实现了它们,我想知道是否有人可以看看,看看我是否朝着正确的方向走?到目前为止,我还没有遇到任何数据库问题,但我希望确保正确实现了事务 在我的代码中,我首先创建用户详细信息并获取ID,然后将该ID插入到用户帐户表中 控制器 if($this->form_validation->run()==false){ $this->index(); }else{

我读过一些关于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');
            }
        }
模型


希望有人能帮我做这个

事务的原因是要执行多个db查询操作,如果任何操作失败,请撤消已经发生的任何操作

在事务块中只执行一个操作,因此事务是无意义的。除此之外,你有这个想法

在您只使用
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"]);
    }

}
上面的事务是合理的,因为我们执行两个db操作,如果其中一个失败,我们不希望对db进行任何更改

在控制器中使用模型/方法

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

在模型代码中设置事务。你能给我举个例子吗?谢谢!我需要做一些调整。
if($this->some_model->save_stuff($the_stuff) === FALSE)
{
    redirect('/wherever');
}
//All OK, proceed
//do other controller things