Arm*_*man 1 php pdo zend-framework zend-db
如果我没有在引导文件中显式地放置默认适配器,Zend_DB_Tables没有默认适配器.我正进入(状态:
Exception information:
Message: No adapter found for Application_Model_MyModel
Run Code Online (Sandbox Code Playgroud)
当我进入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);
}
Run Code Online (Sandbox Code Playgroud)
然后它工作.
这是正常行为还是ZendFramework-1.11.10中的错误?
我的app.ini文件如下所示:
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"
Run Code Online (Sandbox Code Playgroud)
编辑
事实证明我不允许使用_initDb()名称应该是别的东西否则我得到循环依赖问题,如果我做$ this-> bootstrap('db');
它应该工作,而不必明确定义Zend_Db_Table::setDefaultAdapter().您可能只是错过了$this->bootstrap('db')实例化db资源的方法.
这是我在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
Run Code Online (Sandbox Code Playgroud)
这是我在引导程序中的代码
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);
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
通过评论找到了答案,结果如下:
方法 _init + standard resource name(即:db,log,session)在您的引导程序中自动运行,将您的数据库初始化的_init方法的名称更改为除了_initDb应该执行操作之外的其他方法.否则,如果您尝试这样做,$this->bootstrap(*resource name*)您将获得循环依赖.