views:

753

answers:

2

Hi,

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 $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, 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();    
    }
}
A: 

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.

fireeyedboy
You see, I'm providing correct data:`resources.db.adapter=pdo_mysqlresources.db.host=localhostresources.db.username=rootresources.db.password=resources.db.dbname=zend`
Sergio E.
Still the same :)
Sergio E.
A: 

I don't think toArray() is necessary for the factory method.

Try something like this:

db.adapter = PDO_MYSQL
db.params.host = localhost
db.params.username = myname
db.params.password = mypass
db.params.dbname = mydbname

$db = Zend_Db::factory($config->db);
Zend_Db_Table_Abstract::setDefaultAdapter($db);

Nothing more required.

[edit]you're saying you don't have a password on root. place one, add it to your config and try again! you shouldn't leave your db root unprotected even on your localhost. It's just a bad omen. [/edit]

tharkun
@tharkun, now I got Message: No adapter found for Model_DbTable_Bands error :-)
Sergio E.
Yeah, I even tried with password, didn't help :)
Sergio E.
add this in your bootstrap: Zend_Db_Table_Abstract::setDefaultAdapter($db);
tharkun
Added, but when I changed model name and class name to Bands I got Fatal error: Class 'Bands' not found in F:\www\zend\application\controllers\IndexController.php on line 13. My God, it's hard to understand what's going on :(
Sergio E.
ok, change the Model name back to what you had, that is another topic for another time!
tharkun
report back, it'll work ;)
tharkun
Yeah, but now got Message: No adapter found for Model_DbTable_Bands again... Damn. Am I too stupid for ZF?
Sergio E.
definitely not. maybe too impatient ;) you're setting the default adapter correctly after using the factory? Do you use MVC? Maybe post the wohle code of your model in our question!
tharkun
Was using CodeIgniter for a long time, maybe have some bad habbits and definetly yes, I'm impatient. Edited my first question and posted whole codes :)
Sergio E.
if the SET NAMES utf-8 query worked it means that the connection can be established. then probably something with your model is no ok.
tharkun
You can see it now :)
Sergio E.
@Sergio: see my adjusted answer.
fireeyedboy
you need to set the defaultAdapter only once. keep only the second one for the abstract.
tharkun
God... Fatal error: Access to undeclared static property: Bootstrap::$registry in F:\www\zend\application\Bootstrap.php on line 26
Sergio E.