如何在Codeigniter批量插入查询中添加INSERT IGNORE INTO

Ron*_*han 2 php codeigniter

如何使codeigniter函数与insert_batch()一样工作但生成查询像INSERT IGNORE INTO?

我需要INSERT IGNORE INTO因为我的一个表的密钥是唯一的,并且当重复条目出现时它显示错误.

Ron*_*han 5

我在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;
}
Run Code Online (Sandbox Code Playgroud)

要使用此功能

$this->db->custom_insert_batch($table_name, $batch_data);
Run Code Online (Sandbox Code Playgroud)

谢谢 !