SQL Codeigniter: create savepoint and revert to save / rollback multiple transactions from controller

I have processing a db transaction from a page with an ajax request, if the user does not click on the submit button, than rolls back the entire sql transaction that is done with the ajax request (I will manage this, but currently the following logic does not work if updating current page).

I am trying to step through the code but it doesn't work,

function viewPage(){
   $needRollBack=$this->session->userdata('needRollBack');
   if($needRollBack){
      $this->db->trans_rollback();
   }
   $this->db->trans_begin();
   $this->MyModel1->insert(.....);
   $this->MyModel2->insert(.....);
.........................
}

function submitDetails(){
   $this->db->trans_complete();
   $this->session->set_userdata('needRollBack',false);
}

      

when viewPage()

call or refresh the function page again , than if submitDetails()

not called than rollbackback, will all sql-stransaction done by ajax request (starting point from trans_begin ()) be rolled out? Is it possible? Please help me...

+3


source to share


1 answer


There are a few things to find here.

First of all, your database and schema type must support transactions (for example, if you are using the MyISAM table type in MySQL, you will not be able to use transactions).



Second, to check if transactions are running in CI, you write error messages to the error log if $this->db->trans_status() === false

.

Finally, I would use an alternative approach to the above (if possible) - one possible approach would be to store your data in a session (through different stages) and make a call or multiple calls to the database at the endpoint (when the user hits the submit button). You can still use transactions this way and it simplifies the problem.

+1


source







All Articles