Zend中没有默认适配器,除非我明确添加它们.这是功能还是错误? [英] No default adapters in Zend unless I add them explicitly. Is this a feature or a bug?

查看:87
本文介绍了Zend中没有默认适配器,除非我明确添加它们.这是功能还是错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我没有在引导文件中明确放置默认适配器,则Zend_DB_Tables没有默认适配器.我得到了:

If I dont put the default adapter explicitly in the bootstrap file, the Zend_DB_Tables does not have default adapters. I am getting:

Exception information:
    Message: No adapter found for Application_Model_MyModel

当我放入引导程序时:

protected function _initDb(){
     //this returns NULL
    //Zend_Debug::dump(Zend_Db_Table::getDefaultAdapter());
    $resource = $this->getPluginResource('db');
    $db = $resource->getDbAdapter();
     // Now it is not NULL
    //Zend_Debug::dump($db);
    Zend_Db_Table::setDefaultAdapter($db);
}

然后它起作用.

这是正常行为还是ZendFramework-1.11.10中的错误?

Is it normal behavior or a bug in ZendFramework-1.11.10?

我的app.ini文件如下:

my app.ini file looks like:

resources.db.adapter = "Pdo_Mysql"
resources.db.isDefaultTableAdapter = true
resources.db.params.dbname = "mydb"
resources.db.params.username = "myuser"
resources.db.params.password = "mypass"
resources.db.params.host = "localhost"
resources.db.params.charset = "UTF8"

编辑

事实证明,不允许我使用_initDb(),该名称应为其他名称,否则如果我执行$ this-> bootstrap('db');

It turns out that I am not allowed to use _initDb() the name should be something else otherwise I get circular dependence problem if I do $this->bootstrap('db');

推荐答案

它应该工作而不必显式定义Zend_Db_Table::setDefaultAdapter().您可能只缺少实例化db资源的$this->bootstrap('db').

It is supposed to work without having to explicitly define the Zend_Db_Table::setDefaultAdapter(). You might only be missing the $this->bootstrap('db') which instantiate your db resource.

这是我appliction.ini中的代码

Here is the code I have in my appliction.ini

resources.db.adapter                = "Pdo_Mysql"
resources.db.params.host            = "dbhost"
resources.db.params.username        = "username"
resources.db.params.password        = "pass"
resources.db.params.dbname          = "dbname"
resources.db.params.charset         = "utf8"
resources.db.isDefaultTableAdapter  = true
resources.db.profiler.enabled       = true
resources.db.profiler.class         = Zend_Db_Profiler_Firebug

这是我引导程序中的代码

Here is the code I have in my bootstrap

protected function _initDbAdapter()
{
    $this->bootstrap('db');
    $db = $this->getPluginResource('db');

    // force UTF-8 connection
    $stmt = new Zend_Db_Statement_Pdo(
        $db->getDbAdapter(),
        "SET NAMES 'utf8'"
    );
    $stmt->execute();

    $dbAdapter = $db->getDbAdapter();

    // Query profiler (if enabled and not in production)
    $options = $db->getOptions();
    if ($options['profiler']['enabled'] == true
        && APPLICATION_ENV != 'production'
    ) {
        $profilerClass = $options['profiler']['class'];
        $profiler = new $profilerClass('All DB Queries');
        $profiler->setEnabled(true);
        $dbAdapter->setProfiler($profiler);
    }

    Zend_Registry::set('db', $dbAdapter);
}

更新

通过评论找到答案,这是结果:

The answer was found throught the comments, here is the result :

方法_init + standard resource name(即db,日志,会话)在引导程序中自动运行,将用于初始化db的_init方法的名称更改为除_initDb之外的其他名称应该可以解决问题.否则,如果您尝试执行$this->bootstrap(*resource name*),您将获得循环依赖.

Methods _init + standard resource name (ie: db, log, session) are run automatically in your bootstrap, changing the name of your _init method that initialise your db to something else than _initDbshould do the trick. Otherwise if you try to do $this->bootstrap(*resource name*)you will get circular dependancy.

这篇关于Zend中没有默认适配器,除非我明确添加它们.这是功能还是错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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