在 Zend 框架中更改 application.ini 参数的值 [英] change value of application.ini parameters in zend framework

查看:34
本文介绍了在 Zend 框架中更改 application.ini 参数的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 Zend Framework,并在多个数据库中使用基于模块的结构.

I am working on Zend Framework and using module based structure with multiple databases.

我在 application.ini

我的application.ini看起来像

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

我想动态设置 multidb.primary.default 和 multidb.secondary.default 值,以便我可以为不同的模块使用不同的数据库.

I want to set multidb.primary.default and multidb.secondary.default values on fly so that I can use different databases for different modules.

我尝试使用 http://framework.zend 中提到的代码.com/manual/en/zend.config.theory_of_operation.html .

代码片段如下:-

$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;

但它不起作用.

请帮帮我.

推荐答案

您无法动态更改 application.ini 中的参数,因为引导发生在路由/调度之前.配置是在引导过程中加载的,如果你想根据选择的路由更改参数,你已经在路由之后了.

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.

第一个选项

有几个选项.一个 frontController 插件,它在路由完成后(routeShutdown 或更高版本)更改默认数据库适配器.在插件中,您可以执行以下操作:

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);

dbName 的选择(在本例中为次要")取决于您的路由(以及模块/控制器/操作).

The choice of the dbName (in this case 'secondary') depends on your route (and thus module/controller/action).

第二种选择

其次还有另一个选择.您需要使用能够处理每个模块的模块配置的高级模块应用程序资源.有两个人提出了这样的资源插件:Jeroen KeppensMatthijs van den Bos

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

您在 application/modules/mymodule/configs/module.ini 中为一个模块放置一个文件

You place for one module a file in application/modules/mymodule/configs/module.ini

resources.multidb.primary.default = true

在另一个模块中,例如application/modules/anothermodule/configs/module.ini

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.

这篇关于在 Zend 框架中更改 application.ini 参数的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆