database - Drupal 8 - How to switch to external database in custom module?
问题描述
I am trying to switch to and query a external database in a custom Drupal 8 module I have created.
I have added the external database below the native database in 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',
);
I then have a file named BusinessListingDbLogic.php where I make queries to the database :
<?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();
}
I believe my BusinessListingDbLogic class is registered as a service, my business_listing.services.yml looks as follows:
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 }
This code works until I try uncomment Database::setActiveConnection('external');
I then get the following error:
The website encountered an unexpected error. Please try again later.Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'uconomy_external.shortcut_set_users' doesn't exist: SELECT ssu.set_name AS set_name FROM {shortcut_set_users} ssu WHERE ssu.uid = :db_condition_placeholder_0; Array ( [:db_condition_placeholder_0] => 1 )
it looks like the switch is working, but Drupal might be trying to use the external database for its native functionality? I know I also have to switch back to the default database at some point, but I am not sure where to do this?
Any help or advice would be GREATLY appreciate. Kind Regards, Matt
解决方案
似乎不需要调用您当前的连接进行设置,您需要直接使用静态方法Database::setActiveConnection()
。
例如。$this->database->setActiveConnection('external')
变成Database::setActiveConnection('external')
推荐阅读
- html - 如何将左右边距从0移入
- regex - 为什么 regexp_matches 返回不正确的匹配数
- qt - 如何忽略 Qt Creator 上的语义问题
- javascript - 如何在 ExpressJS 中的某些路径而不是其他路径上提供静态文件
- android - “未解决的参考:填充”即使在导入 layout.padding 之后
- perl - 如何使用 perl 从 ps 命令中仅打印“cmd”
- c++ - 在类的最大实例化上抛出错误
- javascript - 如何使用 Ramda 对字符串数组进行数字排序?
- angular - 在类中实例化变量
- google-cloud-platform - 无法从另一个 Linux VM 访问在 Linux VM 中运行的虚拟路由器(使用 EVE-NG)