Read and Write connections

Read connection is not working. Application is executing select queries also from write DB.

CakePHP version - 4.6.1

Example config

'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Postgres',
            'persistent' => env('OPERATING_ENVIRONMENT') === 'DEV',
            'encoding' => 'utf8',
            'timezone' => 'UTC',
            'cacheMetadata' => true,
            'quoteIdentifiers' => true,
            'write' => [
                'username' => 'user',
                'password' => 'passwd',
                'database' => 'db',
                'schema' => 'schema',
                'port' => '5432',
                'host' => 'host'
            ],
            'read' => [
                'username' => 'user',
                'password' => 'passwd',
                'database' => 'db',
                'schema' => 'schema',
                'port' => '5432',
                'host' => 'host'
            ]
        ]

You have to specify which queries should use the read role.

It defaults to the write role.

1 Like

Thanks @KevinPfeifer .

Does any change require in configuration ? mentioned in topic description

No, you need to add ->useReadRole() to the query instances you want to execute via the read rule.

1 Like