I am working on Zend Framework and using module based structure with multiple databases.
I am using multiple databases through multidb approach in application.ini
My application.ini looks like
resources.multidb.primary.adapter = PDO_MYSQL
resources.multidb.primary.host = localhost
resources.multidb.primary.username = root
resources.multidb.primary.password = 123456
resources.multidb.primary.dbname = tubaah_zend
resources.multidb.primary.default = true
resources.multidb.secondary.adapter = PDO_MYSQL
resources.multidb.secondary.host = localhost
resources.multidb.secondary.username = root
resources.multidb.secondary.password = 123456
resources.multidb.secondary.dbname = tubaah
I want to set multidb.primary.default and multidb.secondary.default values on fly so that I can use different databases for different modules.
I tried using the code mentioned in http://framework.zend.com/manual/en/zend.config.theory_of_operation.html .
The code snippet is as following :-
$config = new Zend_Config_Ini(APPLICATION_PATH. '/configs/application.ini', 'development'开发者_运维百科, array('allowModifications' => true));
$config->resources->multidb->primary->default = 0;
$config->resources->multidb->secondary->default = 1;
But it is not working.
Please help me.
You cannot change the parameters in the application.ini on the fly, because bootstrapping occurs before routing/dispatching. The config is loaded during bootstrap and if you want to change the parameters according to the chosen route, you already are after the routing.
First option
There are several options. A frontController plugin which alters the default database adapter after the routing has been done (routeShutdown or later). In the plugin you can do something like this:
$db = Zend_Controller_Front::getInstance()
->getParam('bootstrap')
->getResource('multidb')
->getDb('secondary')
Zend_Db_Table::setDefaultAdapter($db);
The choice of the dbName (in this case 'secondary') depends on your route (and thus module/controller/action).
Second option
Secondly there is another option. You need to use an advanced module application resource which is able to handle module configurations per module. There are two people who have suggested such a resource plugin: Jeroen Keppens and Matthijs van den Bos
You place for one module a file in application/modules/mymodule/configs/module.ini
resources.multidb.primary.default = true
And in another module, e.g. application/modules/anothermodule/configs/module.ini
resources.multidb.secondary.default = true
This has the same result as the first option.
精彩评论