Skip to content

Instantly share code, notes, and snippets.

@eristoddle
Created March 13, 2015 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eristoddle/58ed8ae8966088e300ee to your computer and use it in GitHub Desktop.
Save eristoddle/58ed8ae8966088e300ee to your computer and use it in GitHub Desktop.
Multiple database connections in Phalcon PHP Framework
<?php
//...
foreach ($config->databases as $db => $cred) {
$this->di->set($db, function () use ($cred) {
$adaptor = "\\Phalcon\\Db\\Adapter\\Pdo\\{$cred->adapter}";
return new $adaptor([
'host' => $cred->host,
'username' => $cred->username,
'password' => $cred->password,
'dbname' => $cred->dbname
]);
});
}
//...
<?php
//...
protected $connection;
public function initialize()
{
$defaultConnection = $this->di->get('config')
->get('databases')
->get('default');
$this->connection = $this->di->get($defaultConnection);
}
<?php
return new \Phalcon\Config([
'databases' => [
'default' => 'master',
'master' => [
'adapter' => 'Mysql',
'host' => 'localhost',
'username' => 'root',
'password' => 'root',
'dbname' => 'phalcon',
'persistent' => true,
'charset' => 'utf8'
],
'postgres' => [
'adapter' => 'Postgresql',
'host' => 'localhost',
'username' => 'postgres',
'password' => 'postgres',
'dbname' => 'phalcon',
'persistent' => true,
'charset' => 'utf8'
],
],
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment