Skip to content

belongsTo a different connection doesn't get picked up automatically #17586

Open
@mehov

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

CakePHP Version

3.10

PHP Version

5.6

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions