We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Connecting two different databases

I have a situation:

I want to create connection of two entirely different databases, one for fetching the records and use these records to insert it into another database which is used by my phalcon application.

Note: This is not for master-slave architecture!!!

Well, you'd need to create two services in your DI. It's just that if any component relies on existance of 'db' component, your code might not work as desired, i.e. Models. But in Phalcon everything can be overriden.

can you please share the code example if possible to elaborate your answer.

Well, you'd need to create two services in your DI. It's just that if any component relies on existance of 'db' component, your code might not work as desired, i.e. Models. But in Phalcon everything can be overriden.

edited Jun '16

/**

  • Database connection is created based in the parameters defined in the configuration file
  • NOTE: The mysql libraries and PDO will automatically use Unix sockets if the host is defined as 'localhost'.
  • To force TCP/IP you need to set an IP address 127.0.0.1 in config.ini, which doesn't make sense on a same box, it needs to be changed to a real IPv4 where MySQL instance is hosted remotely. */
$di->setShared('db', function () use ($config) {
    $dbConfig = $config->database->toArray();
    //set your DB as usual....
    }

$di->setShared('dbAudit', function () use ($config) { //this is used for Audit purposes i.e. only inserts
    $dbConfig = $config->databaseAudit->toArray();
    //set your DB as usual....
    }