I have created a SQL Database connection manager that can handle a list of read, write, or named connections and initiate a PDO instance of any one. This is designed to help with load balancing SQL servers to distribute the load.

The idea for this came from the Aura SQL library, which I was really hoping to use, but unfortunately their documentation was quite incorrect… So this just takes one component of their library, the connection manager.

You can get my Connection Manager from This Gist. I’m sorry its not a Composer package, but I likely will do that if some time soon.

And here is a usage example:

<?
$Cons = \Ry167\SQL\Connections();

//Specify multiple write connections
$Cons->add('write','mysql:host=127.0.0.1;dbname=Testing','username','password');
$Cons->add('write','mysql:host=127.0.0.2;dbname=Testing','username','password');

//Specify multiple read connections
$Cons->add('read','mysql:host=127.0.0.2;dbname=Testing','username','password');
$Cons->add('read','mysql:host=127.0.0.3;dbname=Testing','username','password');

//Get a random read and write connection
$ReadCon = $Cons->getRead();
$WriteCon = $Cons->getWrite();

//Set and get a named connection
$Cons->add('testing','mysql:host=127.0.0.3;dbname=Testing','username','password');
$TestingCon = $Cons->get('testing');
?>

You can also change the default options that will get passed to the PDO instance:

<?
//One option at a time
$Cons->setOptions(\PDO::ATTR_ERRMODE,\PDO::ERRMODE_EXCEPTION);

//Multiple options at a time
$Cons->setOptions(array(
	\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
	\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
));
?>

You can find documentation on the different options Here

Additionally, if you have made your own class that extends PDO, you can use them with this connection manager like so:

<?
class MyPDO extends PDO {}
$Con->setClass('MyPDO');
?>

It must be compatible with the same style of initiation as PDO.

I hope this helps some people out, as its something that I couldn’t find a nice stand-alone package for. I will move it to Composer soon hopefully, and here is the link to it one more time, Enjoy - and attribute it to me if you want, its Beerware at this stage.