Welcome Guest, Not a member yet? Register   Sign In
How should I use DB Transactions
#5

(09-02-2016, 12:54 AM)wolfgang1983 Wrote: I have now done it this way but if there is a error on my file upload section how to roll it back also.

PHP Code:
<?php

class Add_user_model extends CI_Model {

 public function 
insert_user($file_name = array()) {
 
$this->db->trans_begin();

 
$user = array(
 
'status' => $this->input->post('status'),
 
'date_added' => date('Y-m-d')
 );

 
$this->db->set($user);
 
$this->db->insert($this->db->dbprefix 'user');

 
$user_id $this->db->insert_id();

 
$options = [
 
   'cost' => 12,
 ];

 
$hash password_hash($this->input->post('password'), PASSWORD_BCRYPT$options);

 
$userdata = array(
 
'user_id' => $user_id,
 
'username' => $this->input->post('username'),
 
'password' => $hash,
 
'firstname' => $this->input->post('firstname'),
 
'lastname' => $this->input->post('lastname'),
 
'email' => $this->input->post('email'),
 
'image' => ($file_name) ? $file_name '',
 
'country_code' => $this->input->post('country'),
 
'timezone' => $this->input->post('timezone')
 );

 
$this->db->set($userdata);
 
$this->db->insert($this->db->dbprefix 'user_data');

 if (
$this->db->trans_status() === FALSE) {
 
   
    $this
->db->trans_rollback();

 } else {

 
   $this->db->trans_commit();
 } 
 }


Controller

PHP Code:
<?php

class Add extends MY_Controller {

 private 
$error = array();

 public function 
__construct() {
 
parent::__construct();
 
$this->load->library('form_validation');
 
$this->load->library('countries');
 
$this->load->library('upload');
 
$this->load->model('admin/user/add_user_model');
 }

 public function 
index() {
 
$data['timezones'] = DateTimeZone::listIdentifiers(DateTimeZone::ALL);

 
$this->form_validation->set_rules('username''Username''trim|required');
 
$this->form_validation->set_rules('firstname''Firstname''trim|required');
 
$this->form_validation->set_rules('lastname''Lastname''trim');
 
$this->form_validation->set_rules('email''Email''trim|valid_email');
 
$this->form_validation->set_rules('password''Password''trim');
 
$this->form_validation->set_rules('confirm_password''Confirm Password''trim|matches[password]');
 
$this->form_validation->set_rules('status''Status''trim|required|callback_status[status]');
 
$this->form_validation->set_rules('country''Country''trim|required|callback_country[country]');
 
$this->form_validation->set_rules('timezone''Time Zone''trim|required|callback_timezone[timezone]');

 
$this->form_validation->set_message('required''<b>{field}</b> must be set!');

 if (
$this->form_validation->run($this)) {

 if (isset(
$_FILES['userfile']) && $_FILES['userfile']['size'] > 0) {

 if (!
is_dir(FCPATH 'uploads/users/' $this->input->post('username') . '/')) {
 
mkdir(FCPATH 'uploads/users/' $this->input->post('username') . '/');
 }
 
 
$config['upload_path'] = './uploads/users/' $this->input->post('username') . '/';
 
               $config['allowed_types'] = 'gif|jpg|png';
 
               $config['max_size'] = 3000;
 
               $config['max_width'] = 0;
 
               $config['max_height'] = 0;

 
               $this->upload->initialize($config);

 
               if (!$this->upload->do_upload('userfile')) {
 
                $this->error['warning'] = $this->upload->display_errors();
 
               }

 
               $upload_data $this->upload->data();
 }

 
$this->add_user_model->insert_user($upload_data['file_name']);
 }

 if (
validation_errors() != false) {
 
$this->error['warning'] = validation_errors('<div class="alert alert-danger">''</div>');
 }

 if (isset(
$this->error['warning'])) {
 
$data['warning_error'] = $this->error['warning'];
 } else {
 
$data['warning_error'] = '';
 }

 
$data['countries'] = $this->countries->get();

 
$data['header'] = Modules::run('admin/common/header/index');
 
$data['footer'] = Modules::run('admin/common/footer/index');

 
$this->load->view('user/add_view'$data);
 }

 public function 
country($str) {
 if (
$str) {
 return 
true;
 } else {
 
$this->form_validation->set_message('country''<b>{field}</b> must be set!');
 return 
false;
 }
 }

 public function 
timezone($str) {
 if (
$str) {
 return 
true;
 } else {
 
$this->form_validation->set_message('timezone''<b>{field}</b> must be set!');
 return 
false;
 }
 }

 public function 
status($str) {
 if (
$str) {
 return 
true;
 } else {
 
$this->form_validation->set_message('status''<b>{field}</b> must be set!');
 return 
false;
 }
 }


If you include the file upload section inside the try catch block then any errors should go the catch portion where you roll back the transaction. I say should but you will want to test by making deliberate errors in the file uploads and in the database inserts.
Reply


Messages In This Thread
RE: How should I use DB Transactions - by Shawn - 09-02-2016, 05:52 PM
RE: How should I use DB Transactions - by Shawn - 09-01-2016, 09:32 PM



Theme © iAndrew 2016 - Forum software by © MyBB