开发者

Zend Framework: Zend_DB Error

I'm trying to learn ZF, but got strange error after 20 minutes :)

Fatal error: Uncaught exception 'Zend_Db_Adapter_Exception' with message 'Configuration array must have a key for 'dbname' that names the database instance'

What does this error mean? I got DB information in my config file:

resources.db.adapter=pdo_mysql
resources.db.host=localhost
resources.db.username=name
resources.db.password=pass
resources.db.dbname=name

Any suggestions?

EDIT:

This is my model file /app/models/DbTable/Bands.php:

class Model_DbTable_Bands extends Zend_Db_Table_Abstract
{
    protected $_name = 'zend_bands';
}

Index controller action:

public function indexAction()
{
    $albums = new Model_DbTable_Bands();
    $this->view->albums = $albums->fetchAll();
}

EDIT All codes:

bootstrap.php

protected function _initAutoload()
{
    $autoloader = new Zend_Application_Module_Autoloader(array(
        'namespace' => '',
        'basePath'  => dirname(__FILE__),
    ));
    return $autoload开发者_高级运维er;
}

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, changed database and provided password:

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


To maybe finally conclude this discussion with the correct solution, here is mine:

application.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

The string params was missing.


Replace

public static function setupDatabase()

With

protected function _initDatabase()

...and try again

I presume you are simply not calling the static method you have defined. But when you create a protected method starting with _init it is automatically executed when bootstrapping.

PS.:
You probably really want to assign a password to the root user of your database, even if it's a test server. Just to be save. If someone is able to access your testserver, you could expose valuable customer info if you don't have a password. root is the first user they will try. Better still, would be to assign a different user/pass combo to each individual database.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜