Doctrine DBAL
Doctrine DBAL is also abstraction layer for database.
Install Ray.DbalModule
with composer.
composer require ray/dbal-module
Install DbalModule
in application module.
use BEAR\Package\AbstractAppModule;
use Ray\DbalModule\DbalModule;
class AppModule extends AbstractAppModule
{
protected function configure()
{
// ...
$this->install(new DbalModule('driver=pdo_sqlite&memory=true'));
}
}
New DI bindings are now ready and $this->db
can be injected with the DbalInject
trait.
use Ray\DbalModule\DbalInject;
class Index
{
use DbalInject;
public function onGet()
{
return $this->db; // \Doctrine\DBAL\Driver\Connection
}
}
Connect to multiple databases
To connect to multiple databases, specify the identifier as the second argument.
$this->install(new DbalModule($logDsn, 'log_db');
$this->install(new DbalModule($jobDsn, 'job_db');
/**
* @Inject
* @Named("log_db")
*/
public function setLogDb(Connection $logDb)
MasterSlaveConnection is provided for master/slave connections.