Fixing ActiveRecord Class for CodeIgniter to use with PostgreSQL

Hi,

If you are using PostgreSQL and execute the following statement, it will defnitely generate error because of some internal properties in the model.

Controller Code


<?
$model
= $this->load->model("MyModel");
$this->mymodel->insert();
?>

where the model code is


<?
Class MyModel extends Model{
  public $username
;
  
public $password;

  public function insert()
  
{
    $this
->db->insert("table",$this);
  
}
}
?>

So guys – Here I fix the Active Record class in CodeIGniter. Here goes the solution. Just replace the existing insert() function in system\database\DB_active_rec.php

Dont worry, it wont hamper the functionality for other DB providers


/**
     * Insert
     *
     * Compiles an insert string and runs the query
     *
     * @access    public
     * @param    string    the table to retrieve the results from
     * @param    array    an associative array of insert values
     * @return    object
     */
    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];
        
}

        /**
         * A small hack for PostgreSQL
         * @author : Hasin Hayder [[email protected]]
         * @since : March 09, 2007
         */
        
if ($this->dbdriver=="postgre"){
            $clone
= $this->ar_set;
            foreach(
$this->ar_set as $key=>$val)
                if (
substr($key,0,1)=="_") unset($clone[$key]);
            
$sql = $this->_insert($this->dbprefix.$table, array_keys($clone), array_values($clone));
        
}
        
else
        
$sql = $this->_insert($this->dbprefix.$table, array_keys($this->ar_set), array_values($this->ar_set));
        
/**
         * End Hack
         */
        
        
$this->_reset_write();
        return
$this->query($sql);
    
}