码迷,mamicode.com
首页 > 其他好文 > 详细

***codeigniter 2.2 affected_rows()返回值不准确

时间:2015-09-21 17:37:50      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

http://blog.icodeu.com/?p=596

问题描述
今天在完成一个项目调用想要检验一下计划插入的数据是否都正常插入了。调用insert_batch()方法插入一百多条数据的时候发现affected_rows()返回值不准确。
问题分析
1.第一步打印last_query()发现插入的数据和affected_rows()数值确实是一样的
2.检查数据库,发现需要插入的一百多条数据也确实正常插入了
3.查看了一下代码,才知道在insert_batch()和update_batch()的时候,如果数据量大于100条,将会分多次插入,每次最多只会插入一百条。
详见下面的代码

1
2
3
4
5
6
// Batch this baby
for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
{
    $sql = $this->_update_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->ar_set, $i, 100), $this->_protect_identifiers($index), $this->ar_where);
    $this->query($sql);
}

影响范围
insert_batch(),update_batch()
解决方案
对插入的数据进行求余然后和affected_rows()进行对比检验插入数据是否正确

github上官方已经修复了这个问题

https://github.com/bcit-ci/CodeIgniter/tree/2.2-stable

 
 
1
2
3
4
5
6
7
    // Batch this baby
$affected_rows = 0;
for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
{
$this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100)));
$affected_rows += $this->affected_rows();
}

***codeigniter 2.2 affected_rows()返回值不准确

标签:

原文地址:http://www.cnblogs.com/kenshinobiy/p/4826288.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!