Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering Zend Database Adapter in Registry

I am looking to register a reference to the main Database Adapter in the Registry during Bootstrapping so it can be used elsewhere in my site (specifically the Authorisation action).

I have implemented an ugly fix where i create a Database Table object and call the getAdapter() method on it and pass through that. However, this is a bad way of doing it and I would like it to be available via the registry.

Does anyone know how to do this? Any help or pointers in the right direction are appreciated!

Cheers Stuart

Ps. Im using Zend Framework 1.8.

like image 637
Stuart Avatar asked May 28 '09 14:05

Stuart


2 Answers

If you're using Zend Framework 1.8+, and created your project with the command line tool, then it's as simple as registering your database settings in your application.ini config file.

resources.db.adapter = "PDO_MYSQL" resources.db.params.host = "your.database.host" resources.db.params.dbname = "database_name" resources.db.params.username = "username" resources.db.params.password = "password" resources.db.isDefaultTableAdapter = true 

If your database settings are preceded by resources.db you won't even need to do anything in your Bootstrap.php file because it will do it for you. Also, by setting the isDefaultTableAdapter setting to true, you can get an instance of your database adapter anywhere in your application.

$dbAdapter = Zend_Db_Table::getDefaultAdapter(); $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter); 
like image 132
Andrew Avatar answered Oct 18 '22 09:10

Andrew


Thanks for the replies. Ive decided to change the accepted answer and post the solution I finally used - which is insanely simple in the end!!

This is basically based on Dcaunt's comment...

In the bootstrap class..

protected function _initDb() {     $resource = $bootstrap->getPluginResource('db');      $db = $resource->getDbAdapter();      Zend_Registry::set("db", $db); } 

Then access that elsewhere with...

$dbAdapter = Zend_Registry::get("db"); 

Thanks for the help and hopefully this helps someone else.

like image 36
Stuart Avatar answered Oct 18 '22 10:10

Stuart