CodeIgniter - ActiveRecords and Oracle - configura

2019-01-29 04:34发布

问题:

I had a problem with ActiveRecords - CI add brackets to the table names - so oracle returns errors.

Here is some info how to fix it :)

first configure your database

My old dns config looks like this

$dsn = array(
    'phptype'   => 'oci8',
    'hostspec'  => '192.xx.215.xx',
    'service'   => 'yyyyy',
    'port'      => '1521',
    'username'  => 'zzzzz',
    'password'  => 'aaaaa'
);

So I added in application\config\database.php this:

$db['oracle']['hostname'] = "192.xx.215.xx/yyyyy";
$db['oracle']['username'] = "zzzzz";
$db['oracle']['password'] = "ttttt";
$db['oracle']['database'] = "dbname.table";
$db['oracle']['dbdriver'] = "oci8";
$db['oracle']['dbprefix'] = "";
$db['oracle']['pconnect'] = FALSE; //must be false
$db['oracle']['db_debug'] = TRUE;
$db['oracle']['cache_on'] = FALSE;
$db['oracle']['cachedir'] = "";
$db['oracle']['char_set'] = "utf8";
$db['oracle']['dbcollat'] = "utf8_general_ci";

in system\database\drivers\oci8\oci8_driver.php replace line

return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);

with

// remove duplicates if the user already included the escape
$str = preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
$str =  rtrim($str,'"');
$str =  ltrim($str,'"');
$str =  str_replace('"."', '.', $str);

return $str;

Now i your model you can call

$this->oracle = $this->load->database('oracle', TRUE);

Now Oracle and Ci should works! :)