I am back with another component for the Ice Cream Framework, one that I am developing ever so slowly as both a learning experience an actual small real world project that helps to understand the different aspects of how frameworks in the land of PHP, work.

The core purpose for writing this new component was to write a very thin wrapper around PDO and allow you to connect to a database (or multiple) and managed those connections as well.

The Github readme has more information.

The core goal here is to create a connection and set it as the default and then allow you to use common PDO methods to interact with the database and then close the connection. At the simplest level it’s:

use IceCreamDatabase\Connect;

$connections = [
  'mysql' => [
    'host' => '127.0.0.1',
    'port' => 3306,
    'database' => '',
    'username' => 'root',
    'password' => 'root',
    'charset' => 'utf8',
  ],
  'pgsql' => [
    'host' => '127.0.0.1',
    'port' => 5432,
    'dbname' => 'scotchbox',
    'username' => 'root',
    'password' => 'root',
    'charset' => 'utf8',
  ],
]

$con = new Connect($connections);

$con->db()->exec( … );

$con->manager()->closeAllConnections();

With db() you can get a specific connection (‘mysql’ or ‘psql’) or the default connection, as we did, which in this case would be mysql.

ATTN!

It's very important that you call $con->manager()->closeAllConnections(); or $con->manager()->closeConnection('mysql'); // or 'pgsql'

Closing a connection is not handled for you, as this is just a very thin wrapper around PDO and I wanted to keep things as simple as possible.