Wednesday, January 5, 2011

CakePHP connect 2, many, multiple database

Tag
CakePHP connect with more than 1 database
CakePHP connect multiple database
CakePHP connect more database
CakePHP connect 2 database
CakePHP connect with 2 database. 1 is CakePHP structured and another 1 is not

The main source is from here
http://blog.4webby.com/posts/view/6/cakephp_models_using_multiple_db_connections

Repeat the same thing
class DATABASE_CONFIG {

var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'your_host',
'login' => 'your_login_1',
'password' => 'your_password_1',
'database' => 'DB_1',
'prefix' => ''
);

var $general_syst = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'your_host',
'login' => 'your_login_2',
'password' => 'your_password_2',
'database' => 'DB_2',
'prefix' => ''
);
}
?>

class User extends AppModel {

var $name = 'User';
var $useDbConfig = 'general_syst';

//your code here
//....
}

class Post extends AppModel {

var $name = 'Post';
var $useDbConfig = 'default';

//your code here
//....
}

$this->Post->bla_bla_bla : data will be retrieved/inserted/updated from DB_1
$this->User->bla_bla_bla : data will be retrieved/inserted/updated from DB_2


Here is some additional
1. I can connect even without make it persistance.

pr($this->Student->useDbConfig);
$this->Student->useDbConfig = 'general_syst';
$ret = $this->Student->query("SELECT * FROM tbl_lecturer");


Note that tbl_lecturer is another non cakePHP database. It used MySQL database and query does take the data nicely

If you want to check the database connection. Refer here http://asipi.blogspot.com/2011/09/cakephp-check-database-configuration.html

No comments: