使用Zend Framework运行时连接到多个数据库

时间:2015-01-02 10:51:50

标签: php zend-framework

在application.ini文件中

resources.multidb.db1.adapter = PDO_MYSQL
resources.multidb.db1.charset = "utf8"
resources.multidb.db1.host = "18.18.13.14"
resources.multidb.db1.username = "test1"
resources.multidb.db1.password = "test1"
resources.multidb.db1.dbname = "test1"
resources.multidb.db1.default = true

resources.multidb.db2.adapter = "Oracle"
resources.multidb.db2.charset = "utf8"
resources.multidb.db2.host = "18.18.13.14"
resources.multidb.db2.port = "1521"
resources.multidb.db2.username = "test2"
resources.multidb.db2.password = "test2"
resources.multidb.db2.dbname = "test2"

resources.multidb.db3.adapter = "Oracle"
resources.multidb.db3.charset = "utf8"
resources.multidb.db3.host = "22.12.45.19"
resources.multidb.db3.port = "1521"
resources.multidb.db3.username = "test3"
resources.multidb.db3.password = "test3"
resources.multidb.db3.dbname = "test3"
resources.multidb.db3.persistent = TRUE;

在模型文件中

我想在运行时使用$ _SESSION ['MISNAME']分配$ _schema $ _SESSION ['MISNAME']具有数据库的详细信息,如db1,db2,db3,如果我直接在$ _schema ='db1'中传递值... ...正在工作

class Application_Model_DbTable_Detailreport extends Application_Model_DbTable_Abstract{
                public $_name;
                public $_schema;
                public $dbArray=array();


                public function __construct($dbArray,$config = array()) {

                    parent::__construct($config);   
                    switch($_SESSION['MISNAME'])
                    {
                        case 'db1':
                            $this->_schema='db1';
                            break;
                        case 'db2':
                            $this->_schema='db2';
                            break;
                        default:
                            $this->_schema='db3';
                }

            }       
    }

1 个答案:

答案 0 :(得分:0)

尝试在Application_Model_DbTable_Abstract类中添加函数

class Application_Model_DbTable_Abstract extends Zend_Db_Table_Abstract
{
    protected function _setupDatabaseAdapter()
    {
        $this->_schema =$_SESSION['MISNAME'];  
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
        $multidb = $bootstrap->getPluginResource('multidb');
        $this->_db = $multidb->getDb($this->_schema); // This is instance for database function connectivity
    }
}

它可能有用......