Zend Framework: Connecting to 2 databases

Posted on: Mar 04, 2009 by wenbert

I was in a situation that I need to connect to 2 different databases.
I started by putting the additional credentials to login into the second database inside my config file: /application/config/app.ini

 
[production]
database.adapter               = "PDO_MYSQL"
database.params.host           = "localhost"
database.params.dbname         = "Database_A"
database.params.username       = "root"
database.params.password       = "1234"
 
psdatabase.adapter             = "PDO_MYSQL"
psdatabase.params.host         = "localhost"
psdatabase.params.dbname       = "Database_B"
psdatabase.params.username     = "root"
psdatabase.params.password     = "1234"
 
[development : production]
database.params.dbname         = "Database_A"
database.params.username       = "root"
database.params.password       = "1234"
 
psdatabase.params.dbname       = "Database_B"
psdatabase.params.username     = "root"
psdatabase.params.password     = "1234"
 
[testing : production]
database.params.dbname         = "Database_A"
database.params.username       = "root"
database.params.password       = "1234"
 
psdatabase.params.dbname       = "Database_B"
psdatabase.params.username     = "root"
psdatabase.params.password     = "1234"

This config file now has 2 sets of database login credentials identified by: database and psdatabase

Now like any other Zend Framework application, we setup the database connection in the bootstrap file. /application/bootstrap.php

//bootstrap code...
$dbAdapter = Zend_Db::factory($configuration->database);
Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
$registry = Zend_Registry::getInstance();
$registry->configuration = $configuration;
$registry->dbAdapter     = $dbAdapter;
 
$psdbAdapter = Zend_Db::factory($configuration->psdatabase);
$registry->psdbAdapter   = $psdbAdapter;
//bootstrap code

What we now have 2 database connections stored in the registry. dbAdapter, which is set as the default adapter and psdbAdapter, which we will be calling everytime we need to call manually when we need to use it.

For example, I have these models.

<?php
/**
 * Psdump.php
 * /opt/apache2/htdocs/apps/org_v2/application/models/Psdump.php
 */
require_once APPLICATION_PATH.'/models/PsdumpTable.php';
class Psdump extends Zend_Db_Table 
{
    protected $_table;
 
    public function getTable()
    {
        if(null === $this->_table) {
            $this->_table = new PsdumpTable;
        }
        return $this->_table;
    }
 
    public function fetchAllEmployees()
    {
        //return $this->getTable()->fetchActiveEmployees();
        return $this->getTable()->getLatestPSDumpTable();
    }
}

And this…

<?php
/**
 * PsdumpTable
 * /opt/apache2/htdocs/apps/org_v2/application/models/PsdumpTable.php
 *
 */
class PsdumpTable extends Zend_Db_Table_Abstract 
{
    protected $_name = 'psdump';
    protected $_psdb;
 
    public function init()
    {
        //load the other adapter
        $this->_psdb = Zend_Registry::getInstance('psdbAdapter')->psdbAdapter;
    }
 
    public function insert(array $data)
    {
        return parent::insert($data);
    }
 
    /**
     * fetch all employees with Status = A
     *
     * @return unknown
     */
    public function fetchActiveEmployees()
    {
        $sql = "SELECT * FROM ".$this->_name." WHERE Status=".$this->_db->quote('A');
        return $this->_db->fetchAll($sql);
    }
 
    /**
     * We are using the psdb here!!!
     *
     * @return unknown
     */
    public function getLatestPSDumpTable()
    {
        $sql = "SHOW tables";
        return $this->_psdb->fetchAll($sql);
    }
}

Subscribe to comments Comment | Trackback |
Post Tags: , , ,

Browse Timeline


Comments ( 5 )

Links for 2009-04…

TODO list 有Firefox扩展,便于管理自己的TODO
一游游戏网 在线Flash游戏。用来消磨时间最好了。
使用Mysql来搭建可扩展的SNS网站(浅谈)谈论了关于MySQL分表的方法。尾部的几个连接相当有用

rollenc拼博 added these pithy words on Apr 14 09 at 3:59 PM

Yet another post intended for myself :P Comments/suggestions — just leave a reply.

Wenbert added these pithy words on Mar 04 09 at 1:24 PM

thanks,一路追寻到此处

yakakey added these pithy words on Oct 27 09 at 4:12 PM

Hi,

Thanks for sharing this. But how to use joins tables across the databases.

I will explain, I have table1 in db1 and table2 in db2. How to join table1 and table2 they have ass_id as common field name.

Thanks in Advance.

PixelMaker added these pithy words on Mar 23 10 at 2:17 AM

Hmmm. I do not know the recommended way to do it but the way would be something like this (the dirty way):

$sql = "SELECT a.*, b.* FROM database1.table1 a 
LEFT JOIN database2.table2 b ON b.id=a.ass_id
WHERE a.status=".$this->_db->quote('A');
 
return $this->_db->fetchAll($sql);

It is basically just a table join between two tables. The SQL output would be something like:

SELECT a.*, b.* 
FROM database1.table1 a 
LEFT JOIN database2.table2 b ON b.id=a.ass_id 
WHERE a.STATUS = "A"
Wenbert added these pithy words on Mar 23 10 at 3:09 AM

Add a Comment


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">


© Copyright 2007 eKini Web Developer Blog . Thanks for visiting!