1

我正在尝试学习 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();
}

编辑所有代码:

引导程序.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);
}

索引控制器.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

模型/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();    
    }
}
4

3 回答 3

1

为了最终以正确的解决方案结束这个讨论,这是我的:

应用程序.ini:

resources.db.adapter = mysqli
resources.db.params.dbname = zftutorial
resources.db.params.host = localhost
resources.db.params.username = juuro
resources.db.params.password = test

字符串params丢失了。

于 2013-03-21T16:02:34.897 回答
0

Seems that you are not running the app with the correct value in the APPLICATION_ENVenvironment var and then it trying to load a different part of the config file instead of development:production

Check that you are running your web application under the correct environment var

  • For Apache put this inside of your <virtualhost>

    SetEnv APPLICATION_ENV development

  • Also you can try to force de APPLICATION_ENV var in your index.php

于 2012-09-15T21:31:37.493 回答
0

代替

public static function setupDatabase()

protected function _initDatabase()

...然后再试一次

我认为您根本没有调用您定义的静态方法。但是当你创建一个受保护的方法时,_init它会在引导时自动执行。

PS.:
您可能真的想为数据库的 root 用户分配一个密码,即使它是一个测试服务器。只是为了得救。如果有人能够访问您的测试服务器,如果您没有密码,您可能会暴露有价值的客户信息。root是他们将尝试的第一个用户。更好的是,为每个单独的数据库分配不同的用户/通行证组合。

于 2010-01-25T02:10:50.047 回答