Welcome Guest, Not a member yet? Register   Sign In
Fixing ActiveRecord Class for CodeIgniter
#1

[eluser]Unknown[/eluser]
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);
}
#2

[eluser]RonPerrella[/eluser]
Thanks for posting that "patch" - it worked great for me on postgres 8.2 and PHP5.

Now, I'm trying to keep CI from including my 'id' column in inserts since it is declared as the primary key and uses a default sequence to populate.

Did you have to do anything special for this kind of situation?
#3

[eluser]RonPerrella[/eluser]
Actually, I was able to simply change your code to:

/**
* A small hack for PostgreSQL (from Forum http://ellislab.com/forums/viewthread/65106/)
* @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 (($key == 'id') || (substr($key,0,1)=="_"))
{
unset($clone[$key]); // don't include fields starting with '_'
}
}
$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
*/


The additional "$key == 'id'" code takes the id column out of the insert.

Thanks for the insight.

-Ron




Theme © iAndrew 2016 - Forum software by © MyBB