我們正在使用Zend Framework 2創建一個具有多個數據庫的Web應用程序。我有一個加載所有客戶信息的core
數據庫。該數據庫包含customer
表。客戶表中的字段是::PHP - 在Zend框架中處理多個數據庫的最佳方式2
- ID
- 用戶名
- 密碼
- 數據庫名稱
- CUSTOMER_NAME
- ................ ...
當客戶登錄時,我必須從core
數據庫加載他的數據庫名稱,然後馬上查詢請求到數據庫。
我不能有多個適配器,因爲所有客戶都有自己的數據庫,我必須從customer
表中加載core_db
!
我以爲我會用表名前綴數據庫名稱。
我Module.php
試過這樣:
"CategoryTableGateway" => function ($sm) {
$dbAdapter = $sm->get("Zend\Db\Adapter\Adapter");
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Category());
return new TableGateway("databasename.category", $dbAdapter, null, $resultSetPrototype);
}
我曾在我的config\autoload\database.global.php
配置缺省數據庫這樣的:
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=core_db;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
)
我有一個像例外:
Base table or view not found: 1146 Table 'core_db.databasename.category' doesn't exist.
然後,我從config\autoload\database.global.php
刪除dbname=core_db
。現在
,我得到了另一個異常,如:
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
讓我怎麼處理這種情況在Zend框架2.我是新來的Zend Framework 2
Edit
:我得到了解決我自己的問題。
要連接到另一個模式的表,您需要通過TableIdentifier
而不是表!
例如,
相反的:
$CategoryTableGateway = new TableGateway("category", $dbAdapter, null, $resultSetPrototype);
你要做的:
$CategoryTableIdentifier = new TableIdentifier('category','dbname');
$CategoryTableGateway = new TableGateway($CategoryTableIdentifier, $dbAdapter, null, $resultSetPrototype);
希望工程!
感謝您的建議。你能舉一些例子嗎?這將不勝感激。 –
什麼的例子? –
忘記電子商務的一部分。我改變了這個問題。這是我的錯誤。我的問題更多的是關於處理多個數據庫! –