php - Codeigniter 3 consecutive transactions not working -
some context first. have controller manage sites. sites can participate study. when site participates study, empty survey object created. participation has link survey.
simplified version of controller code:
$survey = new survey(); //set variables $savedsurvey = $this->surveymodel->persist($survey); if ($savedsurvey) $participation->survey_id = $savedsurvey->id; $savedparticipation = $this->participationmodel->persist($participation);
the persist function straightforward , looks same models.
$sql = "insert 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 straightforward transaction use, described in ci documentation. php error log shows survey inserted , participation inserted successfully. however, mysql logs paint different story:
55 query start transaction 55 query insert survey (survey_class_id, status, created_at) values ('2', 1, now()) 55 query commit 55 query insert participation (study_id, site_id, fs_folder_id, survey_id, reference, created_at) values (1, '33', null, 49, 'redville', now()) 55 quit
not participation insert have no transaction start , no commit, code returns successful operation (quite misleading) , query without transaction not insert. there no participation record in table auto index incremented. it's if second persist failed , there transaction start , rollback not show in mysql log. if manually copy query in mysql log , execute it, insert works , record added table.
i have asked on ci forums , searched similar problems haven't found anything.
- if don't use transactions, works (not solution me)
- i have tried both automatic ci transactions , manual ones same results.
- it isn't model issue. if instead of inserting survey , inserting participation try insert 2 participations, first 1 added.
- i didn't have bug in ci v2 makes me wonder if it's bug v3 find unlikely nobody have ran issue before it's basic operation.
i ran out of ideas test why doesn't work , use input outside and/or experience way ci3 handles transactions.
bug disappeared when upgrading version of php 5.5.10 5.6.10
in order try more accurate estimate of version fixing bug tried php 5.5.22 works (couldn't between , 5.5.10 mamp).
i checked php changelog , noticed version 5.5.12:
mysqli: fixed problem in mysqli_commit()/mysqli_rollback() second parameter (extra comma) , third parameters (lack of escaping).
which doesn't link further details thing in change logs seems related issue , matches versions have bug (before 5.5.10 & after 5.5.22).
Comments
Post a Comment