iam using zend framework to build a REST web service and i am using modules to separate my api versions.
Now, i want to have a separate configuration file for each of my module (v1 and v2), mainly for specifying separate database connections.
I had a directory structure like this:
- application
- modules
- v1
- controllers
- models
- views
- configs
- module.ini
- v2
- controllers
- models
- views
- configs
- module.ini
- configs
- application.xml
- library
I already have the database connection mentioned in my "application.ini" inside application/configs. I read here about module specific confurations and tried it.
I removed these database params from application.ini and put it in module.ini:
[production]
resources.db.adapter = PDO_MYSQL
resources.db.params.host = 127.0.0.1
resources.db.params.username = myuser
resources.db.params.password = mypwd
resources.db.params.dbname = my_db
resources.db.params.profiler.enabled = "true"
resources.db.params.profiler.class = "Zend_Db_Profiler_Firebug"
.....
But i got an error saying "No adapter found..." when i accessed database in my module's controller. Please help...
In the bootstrap, you can set your database connections:
Each connection is specified in a separate .ini file in my case. I find this pretty intuitively organised. A database ini file does not require the
resources.db.whatever
names. Mine go like this:Once you have multiple databases set up like this, when creating a model (in any module you wish), you can inform ZF about the database you would like to use:
This will be your default adapter. In case you need to use two databases in the same query, start your model's function with:
Now, you can write your query using the two databases this way:
As I suggested in a comment, you can also use module-specific bootstraps. The structure would go like this:
The module-specific bootstraps are constructed pretty much like any other bootstrap, but they affect the module in question. The class names are typically prefixed with the modules names, e.g.:
I generally try not to use module-specific bootstraps because they are all launched with each request (Zend Framework 2 is supposed to correct this), running functions that aren't necessary for your current module. Anyway, I found a module-specific bootstrap in one of my modules, which contains something like this:
That's pretty much it. I hope it helps.
The solution (My_App) you refer to in your question does not require any additional configuration for module specific database connections, or any other module specific configuration (except for routes). All you need to do is to declare a MultiDb resource in the application.ini as db1. Then you can declare any module specific database resource in the requested module's respective module.ini as db2, db3, db4... etc... you do not need any additional configuration. I placed an example in the download file at my github. Not to disrespect the response by "mingos" above but there's no need for any additional code in My_App.
Here's the exact verbage taken from the download (application.ini):
Then it declares a single db resource as an example in the download. Just change it to a multi db resource. Declare the application wide needed db resource in application.ini, and any additional db resource that is needed for any specific module in their respective module.ini files. It's straightforward. That's all you need to do. Once you understand the logic behind My_App, you will see it's very powerful.
osebboy's solution is not configured to have a configuration file other than .ini. You have application.xml. It seems like the initial setup is incorrect depending on osebboy's solution.
I suggest that you download the source from his github and set it up that way. Also read his blog post about it.