Some context first. I have a controller to manage sites. Sites can participate to a study. When a site participates to a study, an empty survey object is created. The participation has a link to that survey.
Simplified version of the controller code:
$survey = new Survey();
//Set some variables
$savedSurvey = $this->Surveymodel->persist($survey);
if ($savedSurvey)
$participation->survey_id = $savedSurvey->id;
$savedParticipation = $this->Participationmodel->persist($participation);
The persist function is fairly straightforward and looks the same for all models.
$sql = "INSERT INTO table (a, b, c) VALUES (?, ?, ?)";
$params = array($object->a, $object->b, $object->c);
$this->db->trans_start();
$this->db->query($sql, $params);
$object->id = $this->db->insert_id();
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE) {
error_log('SURVEY INSERT KO');
return false;
} else {
error_log('SURVEY INSERT OK');
return $object;
}
This is straightforward transaction use, exactly as described in the CI documentation. The PHP error log shows that the survey was inserted successfully and that the participation was also inserted successfully. However, the mysql logs paint a different story:
55 Query START TRANSACTION
55 Query INSERT INTO survey (survey_class_id, status, created_at)
VALUES ('2', 1, NOW())
55 Query COMMIT
55 Query INSERT INTO participation (study_id, site_id, fs_folder_id, survey_id, reference, created_at)
VALUES (1, '33', NULL, 49, 'REDVILLE', NOW())
55 Quit
Not only does the participation insert have no transaction start and no commit, but the code returns a successful operation (quite misleading) and the query without a transaction actually does not do the insert. There is no participation record in the table but the auto index is incremented. It's as if the second persist actually failed and there was a transaction start and a rollback that do not show in the mysql log. However if I manually copy the query in the mysql log and execute it, the insert works and the record is added to the table.
I have asked on the CI forums and searched for similar problems but haven't found anything.
I ran out of ideas to test why this doesn't work and could use some input from someone with an outside look and/or experience with the way CI3 handles transactions.
Bug disappeared when upgrading my version of php from 5.5.10 to 5.6.10
In order to try to get a more accurate estimate of the version fixing the bug I tried PHP 5.5.22 which works (couldn't get anything between that and 5.5.10 for MAMP).
I checked the PHP ChangeLog and noticed this for version 5.5.12:
mysqli: Fixed problem in mysqli_commit()/mysqli_rollback() with second parameter (extra comma) and third parameters (lack of escaping).
Which doesn't link to any further details but is the only thing in the change logs that seems related to my issue and that matches the versions for which I have the bug (before 5.5.10 & after 5.5.22).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With