siteparts / pdoconnections
PDO database connections
v1.0.0
2023-08-23 16:22 UTC
Requires
- php: ^7.0 || ^8.0
Requires (Dev)
- phpstan/phpstan: ^1.10
Suggests
- psr/container: ^1.0 || ^2.0 to use the factory
This package is auto-updated.
Last update: 2025-03-23 20:05:30 UTC
README
Create PDO database connections when needed.
Installation
Via Composer:
$ composer require siteparts/pdoconnections
Usage
use PDO; use SiteParts\Database\Pdo\Connections; $dbConfig = [ 'default' => [ 'dsn' => 'mysql:dbname=foo;host=server1;charset=utf8', 'username' => 'john', 'password' => 'secret1', 'attributes' => [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, ], ], 'bar' => [ 'dsn' => 'mysql:dbname=bar;host=server2;charset=utf8', 'username' => 'jack', 'password' => 'secret2', ], ]; $connections = new Connections($dbConfig); $defaultDb = $connections->getConnection(); // $defaultDb is PDO $barDb = $connections->getConnection("bar"); // $barDb is PDO // Subsequent calls return already existing connection $db = $connections->getConnection("bar"); // $db is the same as $barDb