How to use 2 or multiple connections in Cakephp 3.x?

Asked

Viewed 167 times

1

I’m making a system where it takes information from 2 different databases, how can I take the data and differentiate it into the controller and view of Cakephp 3.x? Thanks for the help.

  • I have an example here... I’ll get it and put it on for you to take a look.

1 answer

0

Inside your Appcontroller, insert the two forms of connection (don’t forget to keep the standard format of cake there in the app.php with the database you will use the most - to make your life easier right?!), then just make the connections using the classes

protected function conectarBancoCliente() {
        $configuration = [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'username' => 'SEUUSERNEME',
            'password' => 'SUASENHA',
            'database' => 'SEUBANCO',
            'encoding' => 'utf8',
            'timezone' => 'UTC',
            'cacheMetadata' => true,
            'quoteIdentifiers' => false,
            'log' => false,
            'url' => env('DATABASE_TEST_URL', null),
        ];
        ConnectionManager::config('default', $configuration);

        $conn = ConnectionManager::get('default');

        return $conn;
    }

protected function conectarBancoAdm() {
    $configuration = [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'username' => 'SEUUSERNAME',
        'password' => 'SUASENHA',
        'database' => 'SEUBANCO',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
        'quoteIdentifiers' => false,
        'log' => false,
        'url' => env('DATABASE_TEST_URL', null),
    ];
    ConnectionManager::config('adminitrador', $configuration);

    $conn = ConnectionManager::get('administrador');

    return $conn;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.