【发布时间】:2010-01-25 01:52:57
【问题描述】:
我正在尝试学习 ZF,但 20 分钟后出现奇怪错误:)
Fatal error: Uncaught exception 'Zend_Db_Adapter_Exception' with message 'Configuration array must have a key for 'dbname' that names the database instance'
这个错误是什么意思?我的配置文件中有数据库信息:
resources.db.adapter=pdo_mysql
resources.db.host=localhost
resources.db.username=name
resources.db.password=pass
resources.db.dbname=name
有什么建议吗?
编辑:
这是我的模型文件/app/models/DbTable/Bands.php:
class Model_DbTable_Bands extends Zend_Db_Table_Abstract
{
protected $_name = 'zend_bands';
}
索引控制器操作:
public function indexAction()
{
$albums = new Model_DbTable_Bands();
$this->view->albums = $albums->fetchAll();
}
编辑 所有代码:
bootstrap.php
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => dirname(__FILE__),
));
return $autoloader;
}
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
}
public static function setupDatabase()
{
$config = self::$registry->configuration;
$db = Zend_Db::factory($config->db);
$db->query("SET NAMES 'utf8'");
self::$registry->database = $db;
Zend_Db_Table::setDefaultAdapter($db);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
}
IndexController.php
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$albums = new Model_DbTable_Bands();
$this->view->albums = $albums->fetchAll();
}
}
configs/application.ini,更改数据库并提供密码:
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
db.adapter = PDO_MYSQL
db.params.host = localhost
db.params.username = root
db.params.password = pedro
db.params.dbname = test
models/DbTable/Bands.php
class Model_DbTable_Bands extends Zend_Db_Table_Abstract
{
protected $_name = 'cakephp_bands';
public function getAlbum($id)
{
$id = (int)$id;
$row = $this->fetchRow('id = ' . $id);
if (!$row) {
throw new Exception("Count not find row $id");
}
return $row->toArray();
}
}
【问题讨论】:
-
你能展示你实例化数据库适配器的代码吗?
Zend_Db::factory( /* your config settings */ )之前和包括的代码(我想) -
给你,函数 :) public static function setupDatabase() { $config = self::$registry->configuration; $db = Zend_Db::factory($config->db->adapter, $config->db->toArray()); $db->query("SET NAMES 'utf8'");自我::$registry->数据库 = $db; Zend_Db_Table::setDefaultAdapter($db); PS我不知道如何在评论中包含代码,任何人都可以解决它吗?
-
此外,您显示的配置信息是文字信息吗?如果是这样,我不认为您的数据库名称是
name是吗?更改它以表示您的数据库名称。 (当然还有凭据的用户名和密码) -
代码看起来没问题。请参阅我之前的假设。 (对 cmets 中的代码使用反引号 `。)
-
这是我的测试服务器数据库名称是 zend,用户名是 root 没有密码。不知道有什么问题。有什么想法吗?