change value of application.ini parameters in zend framework

佐手、 提交于 2020-01-06 06:01:40

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/4508556/change-value-of-application-ini-parameters-in-zend-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!