-
Notifications
You must be signed in to change notification settings - Fork 0
Database connection
PDO and Redis is the only supported database interfaces on the Sonic. PDO can be used for PostgreSQL, MySQL, Oracle, SQL Server and SQLite.
PDO supported database drivers are can be configured from app/Config/pdo.php file:
return [
'default' => [
'dsn' => getenv('PGSQL_DSN') ?: 'pgsql:host=localhost;port=5432;dbname=sonic;user=dual;password=1234',
'username' => null,
'password' => null,
]
];For the Redis, app/Config/redis.php file should be configured as:
return [
'default' => [
'address' => getenv('REDIS_ADDRESS') ?: '127.0.0.1',
'port' => getenv('REDIS_PORT') ?: '6379',
]
];In the example above, default configuration parameters for PDO and Redis were initialized, and the configuration values are trying to get it from environment file first, otherwise the default values are using.
On a handler, we can access the PDO instance like this:
$db = \Sonic\Connection\DB::getInstance();
echo $db->query("SELECT current_database()")->fetchColumn(0);We can also set multiple PDO configuration options by the naming these configurations:
return [
'mars' => [
'dsn' => 'pgsql:dbname=mars;user=dual;password=1234'
],
'mercury' => [
'dsn' => 'pgsql:dbname=mercury;user=dual;password=1234'
],
];To switch between PDO instances by the configuration options, just pass the name to the getInstance():
$db = \Sonic\Connection\DB::getInstance('mercury');
echo $db->query('SELECT current_database()')->fetchColumn(0);It also works with Redis instances:
$redis = \Sonic\Connection\Redis::getInstance('mercury');
$redis->set('foo', 'bar');
echo $redis->get('foo');