Skip to content

Instantly share code, notes, and snippets.

@localheinz
Created December 20, 2012 20:25
Show Gist options
  • Save localheinz/4348241 to your computer and use it in GitHub Desktop.
Save localheinz/4348241 to your computer and use it in GitHub Desktop.
Example of how to wire up a database adapter with ZF2
<?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',
),
);
<?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' => '',
),
// ...
);
<?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