Codeigniter 2.1-插入批处理后返回ID [英] Codeigniter 2.1 - return ids after insert batch
问题描述
如何在insert_bunch之后返回所有ID?功能:
How can I return all IDs after insert_bunch? Function:
public function insert_slike($id, $slike, $folder, $polje, $tabela)
{
$slike = explode(',', $slike);
$i = 1;
$data = array();
foreach ($slike as $slk) {
$this->img_upload_resize($slk, $folder);
$data[] = array(
$polje => $id,
'path' => $slk
);
$i++;
}
$this->db->insert_batch($tabela, $data);
}
推荐答案
这是一种有效的方法:
由于您的记录具有结果ID,因此我假设它们也自动递增.
Since your records have result ID's, I'm going to assume they also auto-increment.
在这种情况下,您可以执行此操作,并且仍然使用insert_batch.
If this is the case, you do this and still use insert_batch.
这是您的工作:
1..您需要对要插入的项目进行计数:
1. You take a count of the items you are inserting:
$count = count($data);
2..运行您的批量插入:
2. Run your batch insert:
$this->db->insert_batch($table, $data);
3 .获取您的批次的第一个插入的ID:
3. Get the first inserted ID of your batch:
$first_id = $this->db->insert_id();
4..将计数(减1)添加到插入ID中以获取最后的记录ID.
4. Add the count (minus 1) to your insert ID to get the last records ID.
$last_id = $first_id + ($count-1);
您去了!现在,您将拥有插入记录的第一个和最后一个ID,并且通过扩展,还可以找到介于这两个记录之间的所有其他内容.
There you go! You now have first and last ID's of your inserted records, and by extension, everything else in between.
注意:
一些人问,如果同时发生两个批处理插入,会发生什么情况? InnoDB将智能地管理新行的ID,以防止数据相交,从而使此方法有效.
A few people have asked what might happen if two batch inserts happen simultaneously; InnoDB will intelligently manage the ID's of the new rows to prevent the data intersecting, thus keeping this method valid.
这篇关于Codeigniter 2.1-插入批处理后返回ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!