Welcome Guest, Not a member yet? Register   Sign In
My tweak for multiple database insertion with AR
#1

[eluser]J.Frederic[/eluser]
Hello everyones Confusedmirk:


I want share with you my CI tweak to insert more than one row using the native CI AR class.

usually it's something like that

Code:
$table = 'news';
$data[title] = 'My tweak for multiple database insertion with AR';
$data[text] = 'Hello everyones ... ...';
$this->db->insert($table,$data);

but for some reason, i actually need to use this way :

Code:
$table = 'news';
//first row
$data[0][title] = 'title 1';
$data[0][text] = 'text 1';
//second row
$data[1][title] = 'title 2';
$data[1][text] = 'text 2';
$this->db->insert($table,$data);

So, to make that possible, because is not possible to extend the database class of CI, I had no other choice to hack the native datebase code. I had try to make it clean. Feel free to give me your opinions and/or suggestions.

hack 1 : system/database/DB_active_rec.php | function insert | line(1170)
--------------------------------------------------------------------------
Code:
function insert($table = '', $set = NULL)
    {    
        
        if ( ! is_null($set))
        {
            $this->set($set);
        }
    
        if (count($this->ar_set) == 0)
        {
            if ($this->db_debug)
            {
                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];
        }
        /* HACK JF #65
            call my new _insertArray method if it is a multidimentional array
        */
        if(is_array($set[0])){
            
            $sql = $this->_insertArray($this->_protect_identifiers($table, TRUE, NULL, FALSE),array_keys($this->ar_set[0]),$this->ar_set);
        }else{
            /*original*/
            $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
        }
        /*END OF HACK JF #65*/
        
        $this->_reset_write();
        return $this->query($sql);        
    }


hack 2 : system/database/DB_active_rec.php | function set| line(999)
--------------------------------------------------------------------------
Code:
function set($key, $value = '', $escape = TRUE)
    {
        
        $key = $this->_object_to_array($key);
        
        /* HACK JF #65 */
        /* work on the sublevel array if its a multidimentional and add index to $this->ar_set */
        if (is_array($key[0]))
        {
            $i = 0;
            foreach ($key as $v){
                foreach($v as $kk=>$vv){
                    $this->ar_set[$i][$this->_protect_identifiers($kk)] = $this->escape($vv);
                }
                $i++;
            }
            return $this;
        }
        /* end of HACK JF #65 */
        
        
    
        if ( ! is_array($key))
        {
            
            $key = array($key => $value);
        
        }    

        foreach ($key as $k => $v)
        {
            if ($escape === FALSE)
            {
                $this->ar_set[$this->_protect_identifiers($k)] = $v;
            }
            else
            {
                $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
            }
        }
        
        return $this;
    }


hack 3 : system/database/drivers/mysql/mysql_driver.php | new function _insertArray| add after _insert()
--------------------------------------------------------------------------
Code:
function _insertArray($table, $keys, $values)
    {    
        $query = "INSERT INTO ".$table." ";
        $query .= " (".implode(', ', $keys).") ";
        $query .= " VALUES ";
        foreach ($values as $v){
            $query .= " ( ";
            $query .= implode(', ', $v);
            $query .= " ),";
        }
        return substr($query,0,-1); //delete the last ","
    }

Its that, feel free if you have any questions.

JF


Messages In This Thread
My tweak for multiple database insertion with AR - by El Forum - 10-11-2010, 01:16 PM
My tweak for multiple database insertion with AR - by El Forum - 10-11-2010, 03:56 PM
My tweak for multiple database insertion with AR - by El Forum - 10-11-2010, 03:57 PM
My tweak for multiple database insertion with AR - by El Forum - 10-11-2010, 04:08 PM
My tweak for multiple database insertion with AR - by El Forum - 10-12-2010, 08:48 AM
My tweak for multiple database insertion with AR - by El Forum - 10-12-2010, 09:44 AM
My tweak for multiple database insertion with AR - by El Forum - 10-13-2010, 07:19 AM
My tweak for multiple database insertion with AR - by El Forum - 10-13-2010, 08:27 AM



Theme © iAndrew 2016 - Forum software by © MyBB