Index: library/Zend/Application/Resource/Db.php =================================================================== --- library/Zend/Application/Resource/Db.php (revision 22471) +++ library/Zend/Application/Resource/Db.php (working copy) @@ -158,4 +158,36 @@ return $db; } } + + /** + * Set the default metadata cache + * + * @param string|Zend_Cache_Core $cache + * @return Zend_Application_Resource_Db + */ + public 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; + } } Index: tests/Zend/Application/Resource/DbTest.php =================================================================== --- tests/Zend/Application/Resource/DbTest.php (revision 22471) +++ tests/Zend/Application/Resource/DbTest.php (working copy) @@ -71,6 +71,8 @@ public function tearDown() { + Zend_Db_Table::setDefaultMetadataCache(); + // Restore original autoloaders $loaders = spl_autoload_functions(); foreach ($loaders as $loader) { @@ -145,6 +147,62 @@ $db = $resource->getDbAdapter(); $this->assertTrue($db instanceof Zend_Db_Adapter_Pdo_Sqlite); } + + /** + * @group ZF-10033 + */ + public function testSetDefaultMetadataCache() + { + $cache = Zend_Cache::factory('Core', 'Black Hole', array( + 'lifetime' => 120, + 'automatic_serialization' => true + )); + + $config = array( + 'adapter' => 'PDO_SQLite', + 'params' => array( + 'dbname' => ':memory:', + ), + 'defaultMetadataCache' => $cache, + ); + $resource = new Zend_Application_Resource_Db($config); + $resource->init(); + $this->assertType('Zend_Cache_Core', Zend_Db_Table::getDefaultMetadataCache()); + } + + /** + * @group ZF-10033 + */ + 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); + + $config = array( + 'bootstrap' => $this->bootstrap, + 'adapter' => 'PDO_SQLite', + 'params' => array( + 'dbname' => ':memory:', + ), + 'defaultMetadataCache' => 'database', + ); + $resource = new Zend_Application_Resource_Db($config); + $resource->init(); + $this->assertType('Zend_Cache_Core', Zend_Db_Table::getDefaultMetadataCache()); + } } if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_DbTest::main') {