1

CakePHP 4.0.8

In my config/app_local.php I have 2 different databases defined:

'Datasources' => [

    'default' => [
        'host' => 'localhost',
        'username' => '***',
        'password' => '***',
        'database' => 'db1',
        'url' => env('DATABASE_URL', null),
    ],

    'interface_db' => [
        'host' => 'localhost',
        'username' => '***',
        'password' => '***',
        'database' => 'db2',
        'url' => env('DATABASE_URL', null),
    ]
]

When I execute php bin/cake.php bake model it will list tables in the default database configuration, i.e. any tables in db1.

I can't change to interface_db and bake models for db2. In CakePHP 3 I used the -c flag with bake to change the connection, e.g. bin/cake.php bake model -cinterface_db

This gives an error:

2020-06-05 12:14:53 Error: [Cake\Datasource\Exception\MissingDatasourceConfigException] The datasource configuration "interface_db" was not found. in .../vendor/cakephp/cakephp/src/Datasource/ConnectionManager.php on line 203

The array key interface_db defines the connection to db2.

Why is this occurring? It used to work in Cake 3.

Andy
  • 5,142
  • 11
  • 58
  • 131
  • I think this is a bug in CakePHP 4 so have opened it as an Issue on https://github.com/cakephp/bake/issues/691 – Andy Jun 05 '20 at 13:32

1 Answers1

2

same bug for me, but fixed with app_local.php:

  1. add
use Cake\Database\Driver\Mysql;
use Cake\Database\Connection;

before the return statement

  1. add className & driver to the config:
'className' => Connection::class,
'driver' => Mysql::class,

so in your case :

    'interface_db' => [
            'host' => 'localhost',
            'className' => Connection::class,
            'driver' => Mysql::class,
            'username' => '***',
            'password' => '***',
            'database' => 'db2',
            'url' => env('DATABASE_URL', null),
        ]

and it will work.

className & drivers are defined into app.php for de default, but not for your custom one.

Xbird
  • 31
  • 3