Zend Framework 中基于模块的 application.ini

发布于 2024-11-09 14:57:18 字数 100 浏览 0 评论 0原文

我想在我的应用程序中包含基于模块的 application.ini 。

是否可以?

出现基本要求是因为我有多个数据库取决于模块。

请指导。

I want to have module based application.ini in my application.

Is it possible?

Basic requirement arises because I am having multiple databases depending on modules.

Please guide.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来日方长 2024-11-16 14:57:18

您可以在 application.ini 中使用多个数据库:

resources.db.master.adapter          = "PDO_MYSQL"
resources.db.master.default          = false
resources.db.master.params.dbname    = "db1"
resources.db.master.params.host      = "127.0.0.1"
resources.db.master.params.username  = "root"
resources.db.master.params.password  = ""
resources.db.slave.adapter           = "PDO_MYSQL"
resources.db.slave.default           = true
resources.db.slave.params.dbname     = "db2"
resources.db.slave.params.host       = "127.0.0.1"
resources.db.slave.params.username   = "root"
resources.db.slave.params.password   = ""

并在引导程序中进行初始化:

    public function _initDatabase() {
        $config = $this->getApplication()->getOption('resources');

        $dbArrays = array();
        foreach($config['db'] as $name => $dbConf){
            // Set up database
            $db = Zend_Db::factory($dbConf['adapter'], $dbConf['params']);
            $db->query("SET NAMES 'utf8'");
            $dbArrays[$name] = $db;

            if((boolean)$dbConf['default']){                
                Zend_Db_Table::setDefaultAdapter($db);
            }
            unset($db);
        }

        Zend_Registry::set("db", $dbArrays);
    }

在我的情况下,我总是将每个适配器保存在注册表中,以便稍后可以单独使用它们。
我还创建了一个扩展 Zend_Db_table 的新类,其中我有自己的 getAdapter($name) ,如下所示:

    public function getAdapter($name = null){
        if($name !== null){
            $dbAdapters = Zend_Registry::get('db');
            return $dbAdapters[$name];
        }
        return parent::getAdapter();
    }

在每个模型中我可以这样做:

$this->getAdapter('slave')->fecthAll($sql);
$this->getAdapter('master')->fecthAll($sql);

You can use multiple Db in application.ini:

resources.db.master.adapter          = "PDO_MYSQL"
resources.db.master.default          = false
resources.db.master.params.dbname    = "db1"
resources.db.master.params.host      = "127.0.0.1"
resources.db.master.params.username  = "root"
resources.db.master.params.password  = ""
resources.db.slave.adapter           = "PDO_MYSQL"
resources.db.slave.default           = true
resources.db.slave.params.dbname     = "db2"
resources.db.slave.params.host       = "127.0.0.1"
resources.db.slave.params.username   = "root"
resources.db.slave.params.password   = ""

And initialize in your bootstrap:

    public function _initDatabase() {
        $config = $this->getApplication()->getOption('resources');

        $dbArrays = array();
        foreach($config['db'] as $name => $dbConf){
            // Set up database
            $db = Zend_Db::factory($dbConf['adapter'], $dbConf['params']);
            $db->query("SET NAMES 'utf8'");
            $dbArrays[$name] = $db;

            if((boolean)$dbConf['default']){                
                Zend_Db_Table::setDefaultAdapter($db);
            }
            unset($db);
        }

        Zend_Registry::set("db", $dbArrays);
    }

In my case I always save each adapter in the registry so I can use them separately later.
I also made a new class which extend Zend_Db_table where I have My own getAdapter($name) like so:

    public function getAdapter($name = null){
        if($name !== null){
            $dbAdapters = Zend_Registry::get('db');
            return $dbAdapters[$name];
        }
        return parent::getAdapter();
    }

With that in each model I can do:

$this->getAdapter('slave')->fecthAll($sql);
$this->getAdapter('master')->fecthAll($sql);
嘦怹 2024-11-16 14:57:18

在确定模块之前很久就会读取 application.ini。我建议您忘记 application.ini ,而是尝试编写一个控制器插件,该插件将根据选择的模块加载一些附加配置。

The application.ini is read long before the module is determined. I'd suggest you forget about application.ini and instead try and write a controller plugin that will load in some additional configuration depending on which module was selected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文