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.
Your missing the best thing :)
If you use the Zend_Db_Table models (you should be) etc then you can set up a default adaptor - this way when you instantiate a model the DB connection it taken care off - this way you dont really need to save it in the registry or bother about connection before running a query through the model.
I do save it in the registry for later use if needed though - but I may remove this
I have a method in my bootstrap to add the adapter to the registry. I'd prefer a cleaner solution, but it works:
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.If your database settings are preceded by
resources.db
you won't even need to do anything in yourBootstrap.php
file because it will do it for you. Also, by setting theisDefaultTableAdapter
setting totrue
, you can get an instance of your database adapter anywhere in your application.If you are using Zend Framework 1.8 just do something like this in your controller/action:
My Defaul_Model_Users class would look something like this:
And the part of the model which "interacts" directly with the database tables is found inside DbTable directory will look like this:
Then I would have the same application.ini generated by Zend Framework with this small addition:
That is how I did without without having to change the bootstrap files.
Check the zend-documentation at : 15.5.3.3. Storing a Database Adapter in the Registry
http://framework.zend.com/manual/en/zend.db.table.html
something like that you're looking for?
Edit:
to load your configuration from an ini-file, use:
parse_ini_file($inifile)
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..
Then access that elsewhere with...
Thanks for the help and hopefully this helps someone else.