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

1

I'm doing a system where it takes information from 2 different databases, how can I get the data and differentiate them in the CakePHP 3.x controller and view? Thanks for the help.

    
asked by anonymous 29.12.2016 / 16:52

1 answer

0

Inside your AppController, insert the two forms of connection (do not forget to keep the default format of the cake there in the app.php with the bank you will use more - to make your life easier, right ?!), then it's 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;
}
    
23.01.2017 / 11:19