Created
March 13, 2015 16:39
-
-
Save eristoddle/58ed8ae8966088e300ee to your computer and use it in GitHub Desktop.
Multiple database connections in Phalcon PHP Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
]); | |
}); | |
} | |
//... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//... | |
protected $connection; | |
public function initialize() | |
{ | |
$defaultConnection = $this->di->get('config') | |
->get('databases') | |
->get('default'); | |
$this->connection = $this->di->get($defaultConnection); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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