简体   繁体   中英

Zend Framework - DbAdapter Issue

I am adapting my Zend Framework to work with phpass as according to: http://jonathanstreet.com/blog/adding-phpass-to-zend-framework/

Following his steps, i encountered this error with his Acai_Auth_Adapter_DbTable:

Exception information:

Message: No entry is registered for key 'DbAdapter'

How do i resolve this? Many thanks.

You have to define the database adapter, which the class Acai_Auth_Adapter_DbTable should use by storing a database adapter in Zend_Registry with the key DbAdapter . The best place to do that is your Bootstrap.php :

protected function _initDb()
{
  // add code to create a `Zend_Db_Adapter_xxx`
  Zend_Registry::set('DbAdapter') = $dbAdapter;
}

How to set up the database adapter you find under Zend Framework: Documentation: Zend_Db_Adapter .

Another way is to pass the database adapter as parameter:

// create a database adapter
$authAdapter = new Acai_Auth_Adapter_DbTable( $dbAdpater ):

I would recommend the first way, because then you can access your database adapter anywhere in your application with Zend_Registry::get('DbAdapter') .

assuming your database adapter is setup in your application.ini similar to:

resources.db.adapter = "pdo_Mysql"
resources.db.params.username = "music"
resources.db.params.password = "music"
resources.db.params.dbname = "music"    
resources.db.isDefaultTableAdapter = true // add this line to make this adapter default, important if using more then one adapter.

in your bootstrap.php you can create a new method similar to:

 protected function _initDbAdapter() {//can be named anything as long as it starts with _init
        $db = new Zend_Config($this->getOptions('db'));//get all db adapter resources
        Zend_Registry::set('DbAdapter', $db); //save to Zend_registry key DbAdapter
    }

ByteNudger is correct on how use the registry:
$authAdapter = new Acai_Auth_Adapter_DbTable( $dbAdpater );

However there is a way to avoid all this bootstrap business. If you are only using one database adapter try:

//This will pass in the currently default adapter, if you only have one adapter...
$authAdapter = new Acai_Auth_Adapter_DbTable(Zend_Db::getDefaultAdapter);

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM