i am using a modular architecture with Zend framework, and i want to use the module specific database configuration such that each module’s model employs its own database configuration.
This is my sample application architecture:
- application
- modules
- admin
- controllers
- models
- DbTable
- Users.php
- DbTable
- views
- configs
- module.ini
- admin
- configs
- application.ini
- modules
From the above architecture, i have the module “admin” and i have specified its database configuration in admin/configs/module.ini like:
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "*****"
resources.db.params.password = "*****"
resources.db.params.dbname = "********"
Now I want to use this configuration in the models of “admin” module.
FYI, Iam instantiating the model in the “admin” module’s controller like:
$admin = new Admin_Models_DbTable_Users();
$admin_users = $admin->fetchUsers();
And in the “admin” module’s model, i am executing the queries like:
public function fetchUsers()
{
$sql = "select * from users";
return $this->getAdapter()->fetchAll($sql, array(), Zend_Db::FETCH_OBJ);
}
How can i load the database configuration in admin/configs/module.ini in my “admin” module’s database adapter such that it uses that configuration? Do i need to use Zend Registry or set any options in admin module’s bootstrap file ?
i am following a different approach, as suggested by one of my friends:
In the module’s bootstrap file, i am setting the module’s db adapter into the Registry, and in the module’s model file, i am getting the module’s db adapter from the registry using a custom method: getAdapter()
Code in ….application/modules/admin/Bootstrap.php
Code in …application/modules/admin/DbTable/Users.php
So now the $this->getAdapter() in admin module’s fetchUsers() method will have the db adapter of “admin” module. 🙂