No default adapters in Zend unless I add them expl

2019-07-30 11:44发布

问题:

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

When I put in bootstrap:

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

then it works.

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

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"

EDIT

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

回答1:

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.

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

UPDATE

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

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.