Multiple Database Adapter di Zendframework

Zendframework memfasilitasi untuk penggunaan lebih dari satu database dengan beda jenis. penjelasan koding supaya bisa akses koneksi ke banyak database caranya seperti dibawah ini.

Edit file application.ini menjadi seperti berikut.

[db]
db.adapter = "mysqli"
db.params.host = "localhost"
db.params.username = "root"
db.params.password = ""
db.params.dbname = "hello"

[db1]
db1.adapter = "mysqli"
db1.params.host = "localhost"
db1.params.username = "root"
db1.params.password = ""
db1.params.dbname = "world"

Pada file index.php di folder public tambahkan script berikut.

// get params db1 from application.ini
$config = new Zend_Config_Ini('../application/configs/application.ini', 'db1');

// set database configuration parameters
$adapter = $config->db1->adapter;
$params = $config->db1->params;

// initialize database
$db1 = Zend_Db::factory($adapter, $params);

// set database as default
Zend_Db_Table_Abstract::setDefaultAdapter($db1);

// save database object in Zend_Registry
Zend_Registry::set('db1', $db1);

 

Untuk selanjutnya pemanggilan fungsi database seperti peggunaan biasanya. semoga bermanfaat wassalam.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s