Tuesday, May 20, 2008

Informix and CakePHP 1.2

Today, I tried some kick-ass stuff. Connecting an existing Informix database with cakePHP 1.2. First thing I tried is to use the adodb db wrapper which is built in cakephp. If you need to use adodb, you have to download it from here. Then you extract it and place it in app/vendor.

var $default = array(
'driver' => 'adodb',
'connect' => 'informix',
'persistent' => false,
'host' => 'myserver',
'port' => '9109',
'login' => 'informix',
'password' => 'mypass',
'database' => 'mydb',
'schema' => '',
'prefix' => '',
'encoding' => ''
);

Somehow, this configuration setting works and cakephp's default homepage now shows that it can connect to a database. However, when I try to create a model, and scaffold a controller, it tells me that the table for the model cannot be found.

So then I scour the cyberspace again, looking for the possibility of the existence of a pdo_informix.php file, and I found it hidden in the cakephp google groups, written by Marcelo of San Luis, Argentina. The link can be found here. You need to change the code a little, to set the INFORMIXSERVER environment and get rid of the error

SQLSTATE=IX 000 SQLCODE=-25596


...
function connect()
{
$config = $this->config;
$connect = $config['connect'];
$this->connected = false;

$host = $config['database'] . '@' . $config['host'];
$argHostname = $config['host'];
if ($argHostname) putenv("INFORMIXSERVER=$argHostname");
putenv("INFORMIXSERVER=".trim($argHostname));
if ($config['persistent'])
{
$this->connection = ifx_pconnect($host, $config['login'], $config['password']);
}
else
{
$this->connection = $connect($host, $config['login'], $config['password']);
}

$this->connected = ($this->connection !== false);
return $this->connected;
}
...


Hopefully, the rest of the code can be recycled. I have not tested them yet.

No comments: