0
votes

I am new to Codeigniter and trying to implement the Codeigniter DB transaction on MYSQL database as per the following official guide

https://www.codeigniter.com/user_guide/database/transactions.html

In the guide it suggest that in order to start test mode I need to pass TRUE to the trans_start function, this should rollback the changes in DB however this is NOT happening. My queries are getting committed.

This is my code

$data = array(
            'Name' => $this->input->post('name'),
            'Address' => $this->input->post('addr'),
            'IMEI' => $this->input->post('imei'),
            'DOB' => $this->input->post('dob'),
            'Maritial_Status' => $this->input->post('m_status'),
            'GCM_ID' => $this->input->post('gcm'),
            'Mobile' => $this->input->post('mobile'),
            'Snap' => $this->input->post('snap')
            );

        $this->db->trans_start(TRUE);
        $this->db->set('CreatedOn','NOW()', FALSE);
        $this->db->insert('registered_person', $data);
        $Person_ID_FK=$this->db->insert_id();
        $data1 = array(
            'Person_ID_FK' => $Person_ID_FK,
            'Name' => $this->input->post('r_name'),
            'Number' => $this->input->post('r_number'),
            'Relation' => $this->input->post('relation')
            );

        $this->db->insert('reg_per_emergency_contact', $data1);
        $this->db->set('Person_ID_FK', $Person_ID_FK); 
        $this->db->insert('location');

        $this->db->trans_complete();

        if($this->db->trans_status() === FALSE)
        {
            $resultArray= array('flag'=>10,'status'=>-1);
        }
        else
        {
            $resultArray= array('flag'=>10,'status'=>1,'Person_ID'=>$Person_ID_FK);
        }
        return $resultArray;

P.S. My MYSQL table are using InnoDB Engine.

Any help would be appreciated. Thanks a lot in advance

1

1 Answers

0
votes

This Issue was there before and it was fixed.You can contribute by providing your logs to this issue[1]. For Archive your purpose you can use an alternative method.

    if($this->db->trans_status() === FALSE)
        {
            $resultArray= array('flag'=>10,'status'=>-1);
        }
        else
        {
            $resultArray= array('flag'=>10,'status'=>1,'Person_ID'=>$Person_ID_FK);
        }
        $this->db->trans_rollback(); //Rollback. This will remove your data from the database.
        return $resultArray;

Using $this->db->trans_off();with the alternative is not recommended.

[1]. https://github.com/bcit-ci/CodeIgniter/issues/5