Index: tests/Zend/Application/Resource/MultidbTest.php =================================================================== --- tests/Zend/Application/Resource/MultidbTest.php (revision 22471) +++ tests/Zend/Application/Resource/MultidbTest.php (working copy) @@ -82,6 +82,7 @@ public function tearDown() { Zend_Db_Table::setDefaultAdapter(null); + Zend_Db_Table::setDefaultMetadataCache(); // Restore original autoloaders $loaders = spl_autoload_functions(); @@ -198,7 +199,51 @@ $this->assertEquals($expected, $res->getDb('db2')->getConfig()); } + /** + * @group ZF-10049 + */ + public function testSetDefaultMetadataCache() + { + $cache = Zend_Cache::factory('Core', 'Black Hole', array( + 'lifetime' => 120, + 'automatic_serialization' => true + )); + $options = $this->_dbOptions; + $options['defaultMetadataCache'] = $cache; + $resource = new Zend_Application_Resource_Multidb($options); + $resource->init(); + $this->assertType('Zend_Cache_Core', Zend_Db_Table::getDefaultMetadataCache()); + } + + /** + * @group ZF-10049 + */ + public function testSetDefaultMetadataCacheFromCacheManager() + { + $configCache = array( + 'database' => array( + 'frontend' => array( + 'name' => 'Core', + 'options' => array( + 'lifetime' => 120, + 'automatic_serialization' => true + ) + ), + 'backend' => array( + 'name' => 'Black Hole' + ) + ) + ); + $this->bootstrap->registerPluginResource('cachemanager', $configCache); + + $options = $this->_dbOptions; + $options['defaultMetadataCache'] = 'database'; + $resource = new Zend_Application_Resource_Multidb($options); + $resource->setBootstrap($this->bootstrap); + $resource->init(); + $this->assertType('Zend_Cache_Core', Zend_Db_Table::getDefaultMetadataCache()); + } } if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_LogTest::main') { Index: library/Zend/Application/Resource/Multidb.php =================================================================== --- library/Zend/Application/Resource/Multidb.php (revision 22471) +++ library/Zend/Application/Resource/Multidb.php (working copy) @@ -32,6 +32,8 @@ * * Example configuration: *
+ * resources.multidb.defaultMetadataCache = "database"
+ *
* resources.multidb.db1.adapter = "pdo_mysql"
* resources.multidb.db1.host = "localhost"
* resources.multidb.db1.username = "webuser"
@@ -77,6 +79,11 @@
{
$options = $this->getOptions();
+ if (isset($options['defaultMetadataCache'])) {
+ $this->_setDefaultMetadataCache($options['defaultMetadataCache']);
+ unset($options['defaultMetadataCache']);
+ }
+
foreach ($options as $id => $params) {
$adapter = $params['adapter'];
$default = (int) (
@@ -168,4 +175,36 @@
Zend_Db_Table::setDefaultAdapter($adapter);
$this->_defaultDb = $adapter;
}
+
+ /**
+ * Set the default metadata cache
+ *
+ * @param string|Zend_Cache_Core $cache
+ * @return Zend_Application_Resource_Multidb
+ */
+ protected function _setDefaultMetadataCache($cache)
+ {
+ $metadataCache = null;
+
+ if (is_string($cache)) {
+ $bootstrap = $this->getBootstrap();
+ if ($bootstrap instanceof Zend_Application_Bootstrap_ResourceBootstrapper
+ AND $bootstrap->hasPluginResource('CacheManager')
+ ) {
+ $cacheManager = $bootstrap->bootstrap('CacheManager')
+ ->getResource('CacheManager');
+ if (null !== $cacheManager AND $cacheManager->hasCache($cache)) {
+ $metadataCache = $cacheManager->getCache($cache);
+ }
+ }
+ } else if ($cache instanceof Zend_Cache_Core) {
+ $metadataCache = $cache;
+ }
+
+ if ($metadataCache instanceof Zend_Cache_Core) {
+ Zend_Db_Table::setDefaultMetadataCache($metadataCache);
+ }
+
+ return $this;
+ }
}