I am using Drupal 9.2.10 and would like to update a database entry that is not in the default database.
when using select and join i use:
$query = \Drupal::database()->select('default.table1', 'table1');
$query->leftJoin('db_name2.table2', 'table2', 'table2.id=table1.id);
which works fine
however,
when trying to update a row in the second db by \Drupal::database()->update('db_name2.table2')
Drupal will add the prefix of the default db so that is will actually try to update default.db_name2.table2
which will fail
I could set the active db by \Drupal\Core\Database\Database::setActiveConnection('db_name2');
however would like to avoid it, and use the db name instead
I tried the following with no luck:
\Drupal::database()->update('`db_name2.table2`')`
\Drupal::database()->update('"db_name2.table2"')`
\Drupal::database()->update('{db_name2.table2}')`
Can you help?
In Drupal\Core\Database\Query\Update __toString(), Drupal forces adding the active table by wrapping the table name with curly brackets:
$query = $comments . 'UPDATE {' . $this->connection->escapeTable($this->table) . '} SET ' . implode(', ', $update_fields);
In Drupal\Core\Database\Query\Select toString(), Drupal recognizes the possibility of the table name being a complte db_name.table_name by building the query as follows:
if (strpos($table_string, '.') === FALSE) {
$table_string = '{' . $table_string . '}';
}
Not sure why it's different.
*** Edit ***
Please refer to issue at Drupal site:
https://www.drupal.org/project/drupal/issues/3253479#comment-14334898