Using the following codes we can change the connection to an external database.
$database = Database::getConnection('', $database_key); //'external' $container = \Drupal::getContainer(); $container->set('database', $database);
However, when we want to save an entity we need to either use Node:: create or the entityTypeManager's create method as follows.
$node = \Drupal::entityTypeManager()->getStorage('node')->create( array( 'type' => 'article', 'title' =>'test', 'body' => array( 'value' =>'The body of the content', 'format' => 'full_html', ) ); $node->save();
As far as the entityTypeManager object is already initialized changing the database connection has no effects and the node will be saved in the default database again. So, is there a way to reinitialize the entityTypeManager or to reset the storage method so that it uses the second database?
Any helps will be appreciated. Thanks