I would like to be able to use Codeigniter Database Transactions. But I am new to this and I am unsure if have set it correct to what I am after.
What I am after if there is a error while trying to insert data into my insert_userdata() model function then will roll back data from my insert_user() and also insert_userdata() model functions
I have use the Database Transactions on my controller but not sure if that is correct.
Would like some guidance on this thanks.
Controller
PHP Code:
<?php
class Add extends MX_Controller {
private $error = array();
public function __construct() { parent::__construct(); $this->load->library('form_validation'); $this->load->library('upload'); $this->load->model('admin/user/add_user_model'); }
(09-01-2016, 02:12 AM)wolfgang1983 Wrote: I would like to be able to use Codeigniter Database Transactions. But I am new to this and I am unsure if have set it correct to what I am after.
What I am after if there is a error while trying to insert data into my insert_userdata() model function then will roll back data from my insert_user() and also insert_userdata() model functions
I have use the Database Transactions on my controller but not sure if that is correct.
Would like some guidance on this thanks.
Controller
PHP Code:
<?php
class Add extends MX_Controller {
private $error = array();
public function __construct() { parent::__construct(); $this->load->library('form_validation'); $this->load->library('upload'); $this->load->model('admin/user/add_user_model'); }
if (!$this->upload->do_upload($field)) { $this->error['warning'] = $this->upload->display_errors(); }
}
return !$this->error; } }
public function checkinsertedtodb($inserted) { if (!$inserted) { $this->error['warning'] = 'Opps somthing has gone wrong could not insert data!'; }
return !$this->error; } }
Model
PHP Code:
<?php
class Add_user_model extends CI_Model {
public function insert_user($data) { $this->db->set($data); $this->db->insert($this->db->dbprefix . 'user'); return $this->db->insert_id(); }
public function insert_userdata($data) { $this->db->set($data); $this->db->insert($this->db->dbprefix . 'user_data'); }
}
I suggest that you use a try catch block around the method calls that you want to include in the transaction. That way you can roll back if any kind of error occurs. Also you should check the success of your database calls for success/failure inside the methods and raise an exception on failure.
{ /** * @throws Exception */ public function insert_user($data) { // this one may fail too, so you could do the same as below $this->db->set($data); if (!$this->db->insert($this->db->dbprefix . 'user')) { $error = $this->db->error(); throw new Exception('message ' . $error['code'] . ' ' . $error['message']); } return $this->db->insert_id(); } }
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);
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; } } }
There's only one rule - please don't tell anyone to go and read the manual. Sometimes the manual just SUCKS!
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);
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.