belongsTo
a different connection doesn't get picked up automatically #17586
Open
Description
Description
What do I have
I have a table that uses a non-default connection. To make it worse, the connection name is a variable and is configured.
$table = $tableLocator->get($tableName);
$connection = \Cake\Datasource\ConnectionManager::get($tableDatasourceName);
$table->setConnection($connection);
That table belongsTo
another table in the same database. But neither table has the connection defined in the table class - because again, the connection name varies.
What do I do
$table->find('all', ['contain' => ['AnotherTable']])->first();
What I expect to happen
If the select strategy is join (the default), the other table is looked up within whatever datasource is set for the original table at the moment. (The original table is initialised with a default
connection, which gets updated later using setConnection()
.)
What actually happens
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'default_datasource_database.another_table' doesn't exist
Related
- 3.0 - Associations and different connections #5369 - unlike my issue, OP wants different datasources
- automatic join for belongsTo association with cross databases not work #5058 - basically a wontfix, but from 10 years ago
CakePHP Version
3.10
PHP Version
5.6