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

Multiple database attempt - "Service '[connection name]' was not found in the dependency injection container"

I'm trying to set up multiple database connections, and things aren't entirely clear.

I've seen https://docs.phalcon.io/en/latest/reference/models.html#setting-multiple-databases https://stackoverflow.com/questions/22197678/how-to-connect-multiple-database-in-phalcon-framework

and I have placed this in the top of app/config.php

$di = new Phalcon\DI();
$di->set('[connection name]', function() {
     return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        [connection array]
    ));
});

along with other sets of $di-set 's

before the return new \Phalcon\Config(array( 'database' => array(

and in a model I have:

public function initialize()
{
        $this->setConnectionService('[database name]');
}

I get "Service '[connection name]' was not found in the dependency injection container" back.

What is the right way to add multiple database connections, and in what files, sequence etc? And how would I use those connections, in what files?

Assume you have a master (write) and a slave (read) database/connection. The code below would help you do what you want to achieve.

// Config object
$config = new \Phalcon\Config(
    [
        'dbmaster' => [
            'host'      => 'localhost',
            'username'  => 'masteruser',
            'password'  => '12345',
            'name'      => 'masterdb',
        ],
        'dbslave' => [
            'host'      => 'localhost',
            'username'  => 'slaveuser',
            'password'  => '12345',
            'name'      => 'slavedb',
        ],
    ]
);

// Master
$di->setShared(
    'MasterDatabase', function() use ($config) {
        return new \Phalcon\Db\Adapter\Pdo\Mysql(
            [
                "host"     => $config->dbmaster->host,
                "username" => $config->dbmaster->username,
                "password" => $config->dbmaster->password,
                "dbname"   => $config->dbmaster->name,
            ]
        );
    }
);

// Slave
$di->setShared(
    'SlaveDatabase', function() use ($config) {
        return new \Phalcon\Db\Adapter\Pdo\Mysql(
            [
                "host"     => $config->dbslave->host,
                "username" => $config->dbslave->username,
                "password" => $config->dbslave->password,
                "dbname"   => $config->dbslave->name,
            ]
        );
    }
);

In the model then you do:

public function initialize()
{
    $this->setReadConnectionService('MasterDatabase');
    $this->setWriteConnectionService('SlaveDatabase');

    $this->setSource('mytable');
}

If you use the setConnectionService() you will need to use the name of the service registered with the DI container not the database name. In the case above it would be:

$this->setConnectionService('SlaveDatabase');

Thanks; actually the set up is we have three unique databases that the application could talk to.

It looks like the mvc/multiple from https://github.com/phalcon/mvc/tree/master/multiple could handle that set up, between Modules.php and the config file setup