2016-04-03 63 views
0

我是一名drupal8開發人員。我想連接到模塊中的其他外部數據庫。同樣在D7它是這樣的:如何在運行時在自定義模塊中連接外部數據庫drupal 8

$other_database = array(
     'database' => 'databasename', 
     'username' => 'username', // assuming this is necessary 
     'password' => 'password', // assuming this is necessary 
     'host' => 'localhost', // assumes localhost 
     'driver' => 'mysql', // replace with your database driver 
); 
    // replace 'YourDatabaseKey' with something that's unique to your module 
    Database::addConnectionInfo('YourDatabaseKey', 'default', $other_database); 
    db_set_active('YourDatabaseKey'); 

    // execute queries here 

    db_set_active(); // without the paramater means set back to the default for the site 
    drupal_set_message(t('The queries have been made.')); 

我在D8試過這個,但它是拋出一個錯誤。在這方面你能幫助我嗎?

回答

0

您可以使用db_set_sctive方法在Drupal 8這樣

\Drupal::Database->setActiveConnection($key)

相關問題