【问题标题】:Drupal 8 - How to switch to external database in custom module?Drupal 8 - 如何在自定义模块中切换到外部数据库?
【发布时间】:2019-01-28 09:46:15
【问题描述】:

我正在尝试切换到并在我创建的自定义 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'); 然后我收到以下错误:

网站遇到意外错误。请稍后再试。Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'uconomy_external.shortcut_set_users' 不存在: SELECT ssu.set_name AS set_name 从 {shortcut_set_users} ssu WHERE ssu.uid = :db_condition_placeholder_0;大批 ( [:db_condition_placeholder_0] => 1 )

看起来开关正在工作,但 Drupal 可能正在尝试使用外部数据库来实现其本机功能?我知道我也必须在某个时候切换回默认数据库,但我不确定在哪里执行此操作?

任何帮助或建议将不胜感激。亲切的问候,马特

【问题讨论】:

    标签: database drupal


    【解决方案1】:

    看来不用调用你当前的连接来设置,需要直接使用静态方法Database::setActiveConnection()

    例如。 $this-&gt;database-&gt;setActiveConnection('external') 变为 Database::setActiveConnection('external')

    【讨论】:

    • 你好斜纹布,我已经更新了我的问题。你介意看看,让我知道你的想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    相关资源
    最近更新 更多