在引導,你可以設置你的數據庫連接:
protected function _initDb() {
$config['my_db1'] = new Zend_Config_Ini(APPLICATION_PATH . '/configs/my_db1.ini');
$config['my_db2'] = new Zend_Config_Ini(APPLICATION_PATH . '/configs/my_db2.ini');
$my_db1 = new Plugin_Replication($config['my_db1']->toArray());
$my_db1->query("SET CHARACTER SET utf8;");
$my_db2 = new Plugin_Replication($config['my_db2']->toArray());
$my_db2->query("SET CHARACTER SET utf8;");
Zend_Db_Table::setDefaultAdapter($dmy_db1);
Zend_Registry::set('my_db1', $my_db1);
Zend_Registry::set('my_db2', $my_db2);
}
每個連接在我的情況下,單獨.ini文件中指定。我覺得這很直觀的組織。數據庫ini文件不需要名稱resources.db.whatever
。煤礦是這樣的:
[Master]
host = "xxx"
username = "xxx"
password = "xxx"
dbname = "xxx"
charset = utf8
[Slaves]
first.host = "xxx"
first.username = "xxx"
first.password = "xxx"
first.dbname = "xxx"
first.charset = utf8
second.host = "xxx"
second.username = "xxx"
second.password = "xxx"
second.dbname = "xxx"
second.charset = utf8
一旦你有多個數據庫建立這樣,建立一個模型(你希望的任何模塊中)時,您可以通知ZF有關數據庫,你想用:
protected function _setupDatabaseAdapter() {
$this->_db = Zend_Registry::get('my_db1');
}
這將是您的默認適配器。如果你需要在同一個查詢中使用兩個數據庫,以啓動模型的功能:
public function myAwesomeSqlQuery() {
$db1 = $this->getAdapter()->getConfig(); //default adapter
$db2 = Zend_Registry::get('my_db2')->getConfig(); //additional adapter
現在,您可以使用兩個數據庫編寫查詢是這樣的:
$sql = $this
->select()
->setIntegrityCheck(false)
->from(array('col1' => $db1['dbname'].'.some_column'))
->join(array('col2' => $db2['dbname'].'.some_other_column')),'col1.id = col2.id')
;
正如我建議在評論中,您也可以使用模塊特定的引導程序。該結構將是這樣的:
/application/
-- /modules/
-- /v1/
-- /controllers/
-- /views/
-- /Bootstrap.php
-- /v2/
-- /controllers/
-- /views/
-- /Bootstrap.php
與特定模塊白手起家構造很像其他任何引導,但卻會影響有問題的模塊。類名通常以模塊名稱作爲前綴,例如,:
<?php
class V1_Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
...
}
我一般儘量不使用特定的模塊,白手起家,因爲它們都與每個請求啓動(Zend框架2應該糾正這個),運行的是沒有必要爲您的當前模塊的功能。無論如何,我發現了一個特定模塊的自舉在我的模塊之一,它包含了這樣的事情:
class MyModule_Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initLoggers() {
$my_db1 = Zend_Registry::get('my_db1');
$my_db2 = Zend_Registry::get('my_db2');
$my_db1->setProfiler(new Zend_Db_Profiler_Firebug())->getProfiler()->setEnabled(true);
$my_db2->setProfiler(new Zend_Db_Profiler_Firebug())->getProfiler()->setEnabled(true);
$auth = Zend_Auth::getInstance();
$columnMapping = array('priority' => 'priority' , 'message' => 'message' , 'timestamp' => 'timestamp' , 'username' => 'username');
$logger = new Zend_Log(new Zend_Log_Writer_Db($my_db1, 'logs', $columnMapping));
print_r($auth->getIdentity());
if ($auth->hasIdentity())
$logger->setEventItem('username', $auth->getIdentity()->username);
Zend_Registry::set('logger', $logger);
}
這幾乎是它。我希望它有幫助。
需要更多的信息 - 在這行你的錯誤?這是一個PHP致命錯誤? – emaillenin 2011-03-04 12:43:34
我認爲這只是一個正常的錯誤信息。但錯誤發生後執行停止。 – 2011-03-04 13:05:12
您可以在/public/.htaccess文件中添加以下內容: 'SetEnv APPLICATION_ENV「development」' 然後告訴我們錯誤的詳細信息。 – Perfection 2011-03-04 15:02:21