如何使codeigniter函数与insert_batch()工作相同,但生成像INSERT IGNORE INTO这样的查询?
我需要INSERT IGNORE INTO
,因为我的表的键之一是唯一的,它的显示错误时,重复的条目。
我在codeigniter批插入查询中搜索添加"INSERT IGNORE INTO"而不是"INSERT INTO"的代码,但我没有找到结果。是的,我们可以通过使用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);
谢谢!