INSERT IGNORE using Codeigniter

后端 未结 13 922
眼角桃花
眼角桃花 2020-12-08 19:52

I am trying to insert a few rows into the MySQL table using Codeigniter and Active Records.

PHP Code

$data = array(\'......\');  //          


        
相关标签:
13条回答
  • 2020-12-08 20:27

    solution :

    $sql = $this->db->set($data)->get_compiled_insert($table);
    $sql = str_replace('INSERT INTO', 'INSERT IGNORE INTO', $sql);
    $this->db->query($sql);
    

    works in codeigniter 3.0.6 :)

    0 讨论(0)
  • 2020-12-08 20:28

    I too had same issue what helped me is :

    Open : Codeigniter/system/database/DB_query_builder.php :

    find

    protected function _insert_batch($table, $keys, $values)
        {
            return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
        }
    

    and replace with :

    protected function _insert_batch($table, $keys, $values)
    {
        return 'INSERT IGNORE INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
    }
    
    0 讨论(0)
  • 2020-12-08 20:32

    As I also encountered a similar problem, I finally chose a little bit more "elegant" solution like the one below. A complete insert_batch query that is something that uses Rocket's answer AND transactions:

    $this->db->trans_start();
    foreach ($items as $item) {
           $insert_query = $this->db->insert_string('table_name', $item);
           $insert_query = str_replace('INSERT INTO', 'INSERT IGNORE INTO', $insert_query);
           $this->db->query($insert_query);
        }
    $this->db->trans_complete();
    

    That will also wrap everything on a transaction resulting on a faster query like doing a insert_batch(). Well not as fast as insert_batch() but faster than a single query for each entry of course. I hope it will help someone.

    0 讨论(0)
  • 2020-12-08 20:33

    This is basically a modification of Rocket Hazmat's suggestion, which is great, but doesn't take into account the fact that str_replace operates on the entire string and might inadvertently affect the data.

    $insert_query = $this->db->insert_string('my_table', $data);
    $insert_query = preg_replace('/INSERT INTO/','INSERT IGNORE INTO',$insert_query,1);
    $this->db->query($insert_query);
    
    0 讨论(0)
  • 2020-12-08 20:33

    Avoid duplicate rows by setting a unique key in the database table for at least one of the fields.

    0 讨论(0)
  • 2020-12-08 20:36

    add to file DB_query_builder.php

    this 2 functions

    public function insert_ignore_batch($table = '', $set = NULL, $escape = NULL) {
        if ($set !== NULL)
        {
            $this->set_insert_batch($set, '', $escape);
        }
    
        if (count($this->qb_set) === 0)
        {
            // No valid data array. Folds in cases where keys and values did not match up
            return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
        }
    
        if ($table === '')
        {
            if ( ! isset($this->qb_from[0]))
            {
                return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
            }
    
            $table = $this->qb_from[0];
        }
    
        // Batch this baby
        $affected_rows = 0;
        for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
        {
            $this->query($this->_insert_ignore_batch($this->protect_identifiers($table, TRUE, $escape, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100)));
            $affected_rows += $this->affected_rows();
        }
    
        $this->_reset_write();
        return $affected_rows;
    }
    
    protected function _insert_ignore_batch($table, $keys, $values) {
        return 'INSERT IGNORE INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
    }
    

    using:

    $this->db->insert_ignore_batch('TableName', $data);
    
    0 讨论(0)
提交回复
热议问题