[英]Drupal 8 - How to switch to external database in custom module?
我試圖在我創建的自定義Drupal 8模塊中切換並查詢外部數據庫。
我在settings.php的本地數據庫下面添加了外部數據庫:
// Add second database
$databases['external']['default'] = array(
'database' => 'uconomy_external',
'username' => 'uconomy_admin',
'password' => 'fNjA9kC35h8',
'prefix' => '',
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
然后,我有一個名為BusinessListingDbLogic.php的文件,在其中對數據庫進行查詢:
<?php
namespace Drupal\business_listing;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
/**
* Defines a storage handler class that handles the node grants system.
*
* This is used to build node query access.
*
* This class contains all the logic for interacting with our database
*
* @ingroup business_listing
*/
class BusinessListingDbLogic {
/**
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* @param \Drupal\Core\Database\Connection $connection
*/
public function __construct(Connection $connection) {
$this->database = $connection;
//Database::setActiveConnection('external');
}
/**
* Add new record in table business_listing.
*/
public function add($title, $body, $imageName, $location, $email) {
if (empty($title) || empty($body) || empty($imageName) || empty($location) || empty($email)) {
return FALSE;
}
// add record to business_listing table in database.
$query = $this->database->insert('business_listing');
$query->fields(array(
'title' => $title,
'body' => $body,
'image' => $imageName,
'location' => $location,
'email' => $email
));
return $query->execute();
}
我相信我的BusinessListingDbLogic類已注冊為服務,我的business_listing.services.yml如下所示:
services:
# Service Name.
business_listing.database.external:
class: Drupal\Core\Database\Connection
factory: 'Drupal\Core\Database\Database::getConnection'
arguments: ['external']
# external database dependent serivce.
business_listing.db_logic:
# Class that renders the service.
# BusinessListingDbLogic contains all the functions we use to interact with the business_listings table
class: Drupal\business_listing\BusinessListingDbLogic
# Arguments that will come to the class constructor.
arguments: ['@business_listing.database.external']
# A more detailed explanation: https://www.drupal.org/node/2239393.
# tags:
# - { name: backend_overridable }
在我嘗試取消注釋Database::setActiveConnection('external');
之前,此代碼一直有效Database::setActiveConnection('external');
然后,我得到以下錯誤:
該網站遇到意外錯誤。 請稍后再試。Drupal \\ Core \\ Database \\ DatabaseExceptionWrapper:SQLSTATE [42S02]:找不到基表或視圖:1146表'uconomy_external.shortcut_set_users'不存在:SELECT ssu.set_name AS set_name FROM {shortcut_set_users} ssu WHERE ssu .uid =:db_condition_placeholder_0; 數組([:db_condition_placeholder_0] => 1)
開關似乎工作正常,但是Drupal可能正在嘗試使用外部數據庫來實現其本機功能? 我知道我有時也必須切換回默認數據庫,但是我不確定該在哪里進行?
任何幫助或建議將不勝感激。 親切的問候,馬特
似乎需要調用靜態方法Database::setActiveConnection()
來代替調用當前連接來進行設置。
例如。 $this->database->setActiveConnection('external')
變為Database::setActiveConnection('external')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.