jagguy
jagguy

Reputation: 183

change default db in cakephp3

In a controller I want to change the default database so i can access the new db (db2) from anywhere in the website. The db2 database has the same models but just different data. My code just accesses the other database but doesnt set the new default database to db2 which can be accessed anywhere in the website. I didnt get the answer from the below posts.

This is my controller :

$connection = ConnectionManager::get('db2'); // 'db2' where my second database is configured 
$results = $connection->execute('SELECT * FROM tutors')->fetchAll('assoc');
//this works but doesnt set the default database to db2 everywhere

This is my app.php :

'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',

        //'port' => 'non_standard_port_number',
        'username' => 'root',
        'password' => '',
        'database' => 'aptutori_test',
        'encoding' => 'utf8',
        'timezone' => '+11:00',
        'flags' => [],
        'cacheMetadata' => true,
        'log' => false,

        'quoteIdentifiers' => false,

        'url' => env('DATABASE_URL', null),
    ],

    'db2' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',

        //'port' => 'non_standard_port_number',
        'username' => 'root',
        'password' => '',
        'database' => 'aptutori_testbak',
        'encoding' => 'utf8',
        'timezone' => '+11:00',
        'flags' => [],
        'cacheMetadata' => true,
        'log' => false,

        'quoteIdentifiers' => false,

        'url' => env('DATABASE_URL', null),
    ],

Dynamically change database connection in cakephp 3

http://mark-story.com/posts/view/using-cakephp-and-a-horizontally-sharded-database

Upvotes: 0

Views: 1323

Answers (2)

Seb
Seb

Reputation: 145

You could do this application wide in a Middleware in > cake 3.3, opposed to using a DispatcherFilter, like described in http://mark-story.com/posts/view/using-cakephp-and-a-horizontally-sharded-database.

<?php

namespace App\Middleware;

use Cake\Datasource\ConnectionManager;

class TenantShardMiddleware
{

    public function __invoke($request, $response, $next)
    {

        $tenant = $request->getHeader('MY-tenant');
        ConnectionManager::alias($tenant[0], 'default');
        $response = $next($request, $response);
        return $response;
    }
}

In my example above I'm using a special Request Header to switch databases.

Upvotes: 0

Alex Stallen
Alex Stallen

Reputation: 2252

Use ConnectionManager::alias():

http://api.cakephp.org/3.0/class-Cake.Datasource.ConnectionManager.html#_alias

Fore example this will make all tables that require the default connection to use db2:

ConnectionManager::alias('db2', 'default');

Upvotes: 3

Related Questions