如何在Drupal上定义一个指定模式的外部数据库

时间:2014-06-30 09:31:43

标签: php drupal drupal-7 settings

我想将Drupal连接到外部数据库,我需要指定架构,否则Drupal架构模块默认连接' public'架构。我尝试了以下代码,但没有成功:

 $databases['conservation']['default'] = array(
     'database' => 'conservation',
     'schema' => 'projects_test1',
      'username' => 'xxxxxxxxxxx',
      'password' => 'xxx',
      'host' => 'xxx.xxx.xxx',
      'port' => '5432',
      'driver' => 'pgsql',
      'prefix' => array(
     'default'   => 'projects_test1.',
     'users'     => 'shared.',
     'sessions'  => 'shared.',
     'role'      => 'shared.',
     'authmap'   => 'shared.',
   )

);

我做了一些谷歌搜索,但还没有找到任何人试图这样做。 谢谢 卢卡

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下代码将Drupal站点连接到外部数据库:

   $databases['conservation']['default'] = array(
     'database' => 'conservation',
     'username' => 'xxxxxxxxxxx',
     'password' => 'xxx',
     'host' => 'xxx.xxx.xxx',
     'port' => '5432',
     'driver' => 'pgsql',
     'prefix' => array(
       'default'   => 'projects_test1.',
       'users'     => 'shared.',
       'sessions'  => 'shared.',
       'role'      => 'shared.',
       'authmap'   => 'shared.',
     )    
  );

上面的代码指定了Drupal的保护DB配置。因此,当您需要在模块中查询此其他数据库时,必须使用此函数切换与其的连接

<?php
db_set_active('your_other_db');
?>

完成后,您必须关闭它并恢复到默认数据库连接,以便Drupal能够访问其数据

<?php
db_set_active();
?>
相关问题