cakephp
cakephp copied to clipboard
`belongsTo` a different connection doesn't get picked up automatically
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
- https://github.com/cakephp/cakephp/issues/5369 - unlike my issue, OP wants different datasources
- https://github.com/cakephp/cakephp/issues/5058 - basically a wontfix, but from 10 years ago
CakePHP Version
3.10
PHP Version
5.6
I do realise I'm reporting this for 3.10 which receives security updates only. Let me know if this got fixed in the newer versions?
A workaround to get going:
foreach ($table->associations() as $association) {
$association->setConnection($association->getSource()->getConnection());
}
Let me know if this got fixed in the newer versions?
This has not been fixed to my knowledge.
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().)
Are you expecting that the ORM will detect that the connection is not joinable and change the load strategy when the connection is defined at runtime?
Are you expecting that the ORM will detect that the connection is not joinable and change the load strategy when the connection is defined at runtime?
No no, that sounds too complicated, or I'm not understanding. Sorry about the confusion. Let me try to clarify.
PostsbelongsToUsers, both tables in the same datasource that is notdefault
$posts = $tableLocator->get('Posts');
$connection = \Cake\Datasource\ConnectionManager::get('custom_datasource');
$posts->setConnection($connection);
$posts->find('all', ['contain' => 'Users']):- Reality: errors out because
Usersare looked up in the default datasource - Expectations: because
Postsare incustom_datasource, and belong toUsers, ORM looks upUsersin the datasource ofPosts
- Reality: errors out because
Expectations: because Posts are in custom_datasource, and belong to Users, ORM looks up Users in the datasource of Posts
So are you expecting that the ORM will cascade setConnection() calls to all related models?
So are you expecting that the ORM will cascade setConnection() calls to all related models?
Only if and when it makes sense?
Meaning:
- the select strategy would have been
join - the association type is
belongsTo - the benefit outweighs the risk