ZendFramework framework is used to connect two or more databases.
This example describes how to connect two or more databases using the ZendFramework framework. We will share this with you for your reference. The details are as follows:
Configuration file:
<Db> <adapter> PDO_MSSQL </adapter> <config>
Portal File
// Configure the database connection $ db_config = $ web_config-> db-> config-> toArray (); // var_dump ($ db_config); $ db = Zend_Db :: factory ($ web_config-> db-> adapter, $ db_config); // var_dump ($ db); // exit; // $ db-> query ('set NAMES utf8'); // $ db-> getProfiler ()-> setEnabled (false); Zend_Db_Table: setdefaadapter adapter ($ db );
Here is the default database
Dao. php calls the default database
$db = &$this->getAdapter();
Dao2.php connects to other databases
function init() { $web_config = $this->getCfg(); $this->db2_config = $web_config->db2->config->toArray(); //var_dump($this->db_config); $this->db = Zend_Db::factory($web_config->db2->adapter, $this->db2_config); Zend_Db_Table::setDefaultAdapter($this->db); }public function returnDb(){ return $this->db;}
Call
$db = &$this->getAdapter();
Or connect to the default database.
Direct use
$this->db
You can.
Let's take a look at the complete dao2.php
<? Phpclass dao_dao2 extends Zend_Db_Table {protected $ cfg _; function init () {$ web_config = $ this-> getCfg (); $ this-> db2_config = $ web_config-> db2-> config-> toArray (); // var_dump ($ this-> db_config); $ this-> db = Zend_Db :: factory ($ web_config-> db2-> adapter, $ this-> db2_config); Zend_Db_Table: setDefaultAdapter ($ this-> db);} public function returnDb () {return $ this-> db;} public function getData ($ table, $ whe Re = false, $ order = 'id ASC ', $ pagesize = false, $ offset = false, $ count = false, $ from = false, $ join = false, $ group = false) {// $ this-> db = & $ this-> getAdapter (); $ select = $ this-> db-> select (); if ($ where & is_array ($ where) {foreach ($ where as $ key => $ val) {// print_r ($ where ); if ($ val ['type'] = 1) {$ select-> where ($ key, $ this-> convert2gbk ($ val ['val']);} else {$ select-> orwhere ($ key, $ this-> c Onvert2gbk ($ val ['val ']) ;}} if (! $ From) $ from = '*'; // echo $ select. "<br/>"; if ($ pagesize) {$ select-> limit ($ pagesize, $ offset);} // echo $ select. "<br/>"; if (is_array ($ order) {foreach ($ order as $ value) {$ select-> order ($ value );}} else {$ select-> order ($ order);} // echo $ select. "<br/>"; $ select-> from ($ table, $ count? "COUNT (". $ table. ". id) ": $ from); if (is_array ($ group) {foreach ($ group as $ key => $ val) {$ select-> group ($ val);} if ($ count) {$ result = $ this-> db-> fetchAll ($ select); // echo $ select. "<br/>"; return $ result ;}} else {if ($ count) {$ result = $ this-> db-> fetchOne ($ select ); // echo $ select. "<br/>"; return $ result;} if (is_array ($ join) {foreach ($ join as $ key => $ val) {// $ select-> join ($ key, $ val [0], $ val [1]); $ select-> joinleft ($ key, $ val [0], $ val [1]) ;}// echo $ select. "<br/>"; // echo $ select; exit; $ result = $ this-> db-> fetchAll ($ select ); foreach ($ result as $ key => $ value) {foreach ($ value as $ key2 => $ value2) {$ result [$ key] [$ key2] = $ this-> convert2utf8 ($ value2) ;}} return $ result ;} /*** insert data into the table * array $ adata data * string $ table name * int $ insterid whether to return the insert ID * @ return true or false or int */// @ bianding 2013.11.04 changed mssql in pdo. php lastInsertId () function // @ bianding 2013.11.04 tested mssql. the SELECT method in the lastInsertId () function in php can all be function SaveData ($ adata, $ table, $ insterid = 0, $ aLog = false) {// $ this-> db = & $ this-> getAdapter (); foreach ($ adata as $ key => $ value) {$ adata [$ key] = $ this-> convert2gbk ($ value);} if ($ this-> db-> insert ($ table, $ adata )) {// var_dump ($ this-> db-> getProfiler (); $ insertedID = $ this-> db-> lastInsertId (); if ($ insterid) {return $ insertedID;} else {return TRUE ;}} else {return false ;}} /*** Delete table data ** @ param string $ table name * @ param string $ where 'id = '. $ id condition * @ return true or false */function DelData ($ table, $ where, $ aLog = false) {// $ this-> db = & $ this-> getAdapter (); if ($ this-> db-> delete ($ table, $ where )) {return TRUE;} else {return FALSE ;}} /*** update table data ** @ param string $ table * @ param array $ adata * @ param string $ where 'id = '. $ id * @ return true or false */function UpdateData ($ table, $ adata, $ cond, $ aLog = false) {// $ this-> db = & $ this-> getAdapter (); foreach ($ adata as $ key => $ value) {$ adata [$ key] = $ this-> convert2gbk ($ value);} if ($ this-> db-> update ($ table, $ adata, $ cond )) {return TRUE;} else {return false;} public function clearTable ($ table) {// $ this-> db = & $ this-> getAdapter (); $ result = $ this-> db-> query ('truncate Table '. $ table);} public function executeSql ($ strSql) {// $ this-> db = & $ this-> getAdapter (); $ result = $ this-> db-> query ($ strSql);} function convert2utf8 ($ string) {$ config = $ this-> getCfg (); $ pdoType = $ config-> db-> config-> pdoType; if ($ pdoType = 'dblib') {return iconv ("gbk", "UTF-8 ", $ string);} elseif ($ pdoType = 'sqlsrv') {// $ encode = mb_detect_encoding ($ string, array ('utf-8', "GB2312 ", 'gbk', 'big5'); // echo $ encode; return mb_convert_encoding ($ string, "UTF-8", "UTF-8"); // return $ string ;}} function convert2gbk ($ string) {$ config = $ this-> getCfg (); $ pdoType = $ config-> db-> config-> pdoType; if ($ pdoType = 'dblib') {return iconv ("UTF-8", "gbk", $ string);} elseif ($ pdoType = 'sqlsrv ') {// $ encode = mb_detect_encoding ($ string, array ('utf-8', "GB2312", 'gbk', 'big5'); // echo $ encode; return mb_convert_encoding ($ string, "UTF-8", "UTF-8"); // return $ string ;}} protected function & getCfg () {if ($ this-> cfg _ = null) {$ registry = Zend_Registry: getInstance (); $ this-> cfg _ = $ registry-> get ('web _ config');} return $ this-> cfg _;}}