[英]Codeigniter 3 consecutive transactions not working
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. 这是简单的事务使用,完全如CI文档中所述。 The PHP error log shows that the survey was inserted successfully and that the participation was also inserted successfully.
PHP错误日志显示调查已成功插入,并且还成功插入了参与。 However, the mysql logs paint a different story:
但是,mysql日志描绘了一个不同的故事:
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.
这就好像第二个持久化实际上失败了,并且在mysql日志中没有显示事务启动和回滚。 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.
但是,如果我在mysql日志中手动复制查询并执行它,插入工作并将记录添加到表中。
I have asked on the CI forums and searched for similar problems but haven't found anything. 我已经在CI论坛上询问并搜索了类似的问题,但没有找到任何东西。
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. 我没有想法来测试为什么这不起作用,并且可以使用具有外部外观和/或经验的人对CI3处理事务的方式的一些输入。
Bug disappeared when upgrading my version of php from 5.5.10 to 5.6.10 将我的PHP版本从5.5.10升级到5.6.10时,Bug消失了
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). 为了试图更准确地估计修复bug的版本,我尝试了PHP 5.5.22,它起作用(在MAMP之间无法得到任何东西)。
I checked the PHP ChangeLog and noticed this for version 5.5.12: 我检查了PHP ChangeLog并注意到这个版本为5.5.12:
mysqli: Fixed problem in mysqli_commit()/mysqli_rollback() with second parameter (extra comma) and third parameters (lack of escaping).
mysqli:使用第二个参数(额外的逗号)和第三个参数(缺少转义)修复了mysqli_commit()/ mysqli_rollback()中的问题。
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). 哪个没有链接到任何进一步的细节,但是在更改日志中唯一与我的问题相关并且与我有错误的版本(5.5.10之前和5.5.22之后)匹配的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.