[eluser]MadZad[/eluser]
This is the first time I've tried using CI's transactions, so I'm hoping someone will point out a common pitfall that I blindly walked into.
Here's my model's contructor:
Code:
private $mass_db;
function Person_model() {
parent::Model();
$this->mass_db = $this->load->database("mass", TRUE);
}
And here's the function in that model:
Code:
function save_person($person_id, $save_vals) {
$this->mass_db->trans_start();
$this->mass_db->delete("ada_person", array("person_id" => $person_id));
foreach ($save_vals as $key => $value) {
if (substr($key, 0, 3) == "ada") {
if ($value == 1) {
$ap_data = array("ada_id" => substr($key, 3), "person_id" => $person_id);
$this->mass_db->insert("ada_person", $ap_data);
}
unset($save_vals[$key]);
}
}
$this->mass_db->update('persons', $save_vals, "id = " . $person_id);
$this->mass_db->trans_complete();
return !$this->mass_db->trans_status();
}
So I've got a delete and zero or more inserts on one table, and an update on another table. Works fine under normal conditions.
If I specify
trans_start(TRUE) or force a bad value in for
$person_id, I was expecting the transaction to roll back, and have no changes to the DB. However, the DB updates are made normally, with the delete and inserts happening, and the update failing if I hack in $person_id = 9999.
Caveats - I am using MySQL, but changed both tables to be of type InnoDB. The rest of the code works well enough, so while not the smartest, it's good enough. The application uses multiple DBs, so that's why I'm using
$this->mass_db. This is my first CI project.
Any wisdom and/or guesses how to successfully transactionalize?