[英]How to switch to second database Drupal
I am new to Drupal Developement. 我是Drupal开发的新手。
I have found quite alot of information regarding adding a additional database online, I am just struggling to implement it in my existing code. 我已经找到了很多有关在线添加其他数据库的信息,我只是在努力在现有代码中实现它。
I have a file called BusinessListingDbLogic.php, that currently connects to the native Drupal database and performs queries. 我有一个名为BusinessListingDbLogic.php的文件,该文件当前连接到本机Drupal数据库并执行查询。
I make the connection to the native database using the following code in BusinessListingDbLogic.php 我使用BusinessListingDbLogic.php中的以下代码连接到本机数据库
<?php
namespace Drupal\business_listing;
use Drupal\Core\Database\Database;
class BusinessListingDbLogic {
protected $database;
public function __construct() {
$this->database = Database::getConnection();
//$this->database->setActiveConnection('external');
}
This works fine in that it connects to the native Drupal database without issues, but as soon as I uncomment //$this->database->setActiveConnection('external');
这可以正常工作,因为它可以毫无问题地连接到本地Drupal数据库,但是只要我取消注释
//$this->database->setActiveConnection('external');
, I get ,我得到
Call to undefined method
调用未定义的方法
Drupal\\Core\\Database\\Driver\\mysql\\Connection::setActiveConnection() in Drupal\\business_listing\\BusinessListingDbLogic->__construct()
Drupal \\ business_listing \\ BusinessListingDbLogic-> __ construct()中的Drupal \\ Core \\ Database \\ Driver \\ mysql \\ Connection :: setActiveConnection()
What am I doing wrong here? 我在这里做错了什么? Any help or advice would be GREATLY appreciated.
任何帮助或建议,将不胜感激。 Kind regards, Matt
亲切的问候,马特
Use below code : 使用以下代码:
\Drupal\Core\Database\Database::setActiveConnection('otherdb');
$connection = \Drupal\Core\Database\Database::getConnection();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.