Created
December 20, 2012 20:25
-
-
Save localheinz/4348241 to your computer and use it in GitHub Desktop.
Example of how to wire up a database adapter with ZF2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// config/db.local.php | |
return array( | |
'db' => array( | |
'database' => 'name_of_your_database', | |
'username' => 'user', | |
'password' => 'password', | |
'hostname' => '127.0.0.1', | |
), | |
'db_another' => array( | |
'database' => 'name_of_your_database', | |
'username' => 'user', | |
'password' => 'password', | |
'hostname' => '127.0.0.1', | |
), | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// module/MyNamespace/config/module.config.php | |
return array( | |
// ... | |
'db' => array( | |
'driver' => 'Pdo_Mysql', | |
'characterset' => 'utf8', | |
'database' => '', | |
'username' => '', | |
'password' => '', | |
'hostname' => '', | |
), | |
'db_another' => array( | |
'driver' => 'Pdo_Mysql', | |
'characterset' => 'utf8', | |
'database' => '', | |
'username' => '', | |
'password' => '', | |
'hostname' => '', | |
), | |
// ... | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// module/MyNamespace/Module.php | |
namespace MyNamespace; | |
use Zend\ModuleManager\Feature\ConfigProviderInterface; | |
use Zend\ModuleManager\Feature\ServiceProviderInterface; | |
use Zend\ServiceManager\ServiceManager; | |
class Module implements ConfigProviderInterface, ServiceProviderInterface | |
{ | |
/** | |
* Returns an array with module-specific configuration options. | |
* | |
* @return array | |
*/ | |
public function getConfig() | |
{ | |
return include __DIR__ . '/config/module.config.php'; | |
} | |
/** | |
* Returns an array with service configuration options. | |
* | |
* @return array | |
*/ | |
public function getServiceConfig() | |
{ | |
return array( | |
'factories' => array( | |
/** | |
* Uses the factory class | |
* | |
* @see Zend\Db\Adapter\AdapterServiceFactory | |
*/ | |
'DatabaseAdapter' => 'Zend\Db\Adapter\AdapterServiceFactory', | |
/** | |
* Uses a closure | |
*/ | |
'AnotherDatabaseAdapter' => function (ServiceManager $serviceManager) { | |
$config = $serviceManager->get('Config'); | |
return new \Zend\Db\Adapter\Adapter($config['db_another']); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment