如何使codeigniter函数与insert_batch()相同但生成查询,如INSERT IGNORE INTO?
我需要INSERT IGNORE INTO
,因为我的一张表的密钥是唯一的,并且在重复输入时显示错误。
答案 0 :(得分:5)
我在代码中搜索了添加" INSERT IGNORE INTO"而不是" INSERT INTO"在codeigniter批量插入查询中,但我没有找到结果。 是的,我们可以使用PHP登录进行自定义批量插入查询但是如果你想在codeigniter中使用这个函数。
在(codeigniter / system / database / DB_active.rec.php)中添加此功能。
/*
*Function For Batch Insert using Ignore Into
*/
public function custom_insert_batch($table = '', $set = NULL)
{
if ( ! is_null($set))
{
$this->set_insert_batch($set);
}
if (count($this->ar_set) == 0)
{
if ($this->db_debug)
{
//No valid data array. Folds in cases where keys and values did not match up
return $this->display_error('db_must_use_set');
}
return FALSE;
}
if ($table == '')
{
if ( ! isset($this->ar_from[0]))
{
if ($this->db_debug)
{
return $this->display_error('db_must_set_table');
}
return FALSE;
}
$table = $this->ar_from[0];
}
// Batch this baby
for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
{
$sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
$sql = str_replace('INSERT INTO','INSERT IGNORE INTO',$sql);
//echo $sql;
$this->query($sql);
}
$this->_reset_write();
return TRUE;
}
使用此功能
$this->db->custom_insert_batch($table_name, $batch_data);
谢谢!