简体   繁体   中英

CakePHP 4 - bake won't let me use a different database connection to 'default'

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, ie 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, eg 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.

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.

have you listed the configuration in Datasources, Config/app.php?

It should be the same that the default configuration

Docs: https://book.cakephp.org/4/en/orm/database-basics.html#configuration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM