用于与第二个数据库交互的Drupal模块?

时间:2014-09-11 14:48:57

标签: php mysql drupal

我们的Drupal站点将利用来自第二个MySQL数据库的数据。连接已经在settings.php

我现在想知道是否有可用于与之交互的模块(选择,更新等)。或者至少使用SQL查询的最佳实践是什么 - 带模块或代码的GUI?

一个目标是在屏幕上显示结果,并允许用户过滤行和列。

2 个答案:

答案 0 :(得分:0)

在settings.php中:

$databases = array();

$databases['default']['default'] = array(
  'driver' => 'mysql',
  'database' => 'newdatabase',
  'username' => 'root',
  'password' => 'root',
  'host' => 'localhost',
  'prefix' => '',
  'collation' => 'utf8_general_ci',
);


 $databases['legacy']['default'] = array(
   'driver' => 'mysql',
   'database' => 'olddatabase',
   'username' => 'root',
   'password' => 'root',
   'host' => 'localhost',
   'prefix' => '',
   'collation' => 'utf8_general_ci',
 );

然后在您的代码中,您可以使用db_set_active()函数进行切换。要切换回db_set_active,请将第一个参数设为空。

例如,见下文:

db_set_active('legacy');

然后

db_set_active(enter code here);

将切换回主数据库。

在下面的示例中,我使用for循环和数组从旧数据库和新数据库中获取一系列数据。

      $outputArray = array();


  $data = array(
    'article' => array(
        'number of articles' => array(
          'legacy' => "select count(*) from node WHERE type = 'article';",
          'new' => "select count(*) from node WHERE type = 'article';",
        ),
        'field_byline' => array(
          'default_style' => TRUE,
          ),
        'field_images' => array(
          'fid_style' => TRUE,
         ),
        'field_subtitle' => array(
          'default_style' => TRUE,
          ),
        'field_blurb' => array(
          'default_style' => TRUE,
          ),
        'field_teaser_image' => array(
          'fid_style' => TRUE,
         ),
         'field_rotator_image' => array(
          'fid_style' => TRUE,
         ),
        'field_photo_credit' => array(
          'default_style' => TRUE,
          ),
        'field_videos' => array(
          'fid_style' => TRUE,
         ),
        'field_issue_id' => array(
          'default_style' => TRUE,
          ),
         'field_aol_player' => array(
           'default_style' => TRUE,
         ),
         'field_brightcove_id' => array(
           'default_style' => TRUE,
         ),
         'field_related_articles' => array(
           'legacy' => "select count(DISTINCT(CONCAT(nid, field_related_articles_nid))) from content_field_related_articles WHERE field_related_articles_nid IS NOT NULL;",
          'new' => "select count(*) from field_data_field_related_articles WHERE field_related_articles_target_id IS NOT NULL;",
         ),
    ),
  );


  foreach($data as $type => $type_data) {
    foreach($type_data as $field => $field_data) {

      if(isset($field_data['default_style']) && $field_data['default_style'] == TRUE) {
        $field_data['legacy'] = "select count(DISTINCT(nid)) from content_" . $field . " WHERE " . $field . "_value IS NOT NULL;";
         $field_data['new'] =  "select count(*) from field_data_" . $field . " WHERE " . $field . "_value IS NOT NULL;";
      }

      else if(isset($field_data['fid_style']) && $field_data['fid_style'] == TRUE) {
        $field_data['legacy'] = "select count(DISTINCT(nid)) from content_" . $field . " WHERE " . $field . "_fid IS NOT NULL;";
         $field_data['new'] =  "select count(*) from field_data_" . $field . " WHERE " . $field . "_fid IS NOT NULL;";
      }

      else if(isset($field_data['nid_style']) && $field_data['nid_style'] == TRUE) {
        $field_data['legacy'] = "select count(*) from content_" . $field . " WHERE " . $field . "_nid IS NOT NULL ;";

         $field_data['new'] =  "select count(*) from field_data_" . $field . " WHERE " . $field . "_target_id IS NOT NULL;";

      }      



      db_set_active('legacy');
      $count = (int) db_query($field_data['legacy'])->fetchField();

      db_set_active();
      $count_new =  (int) db_query($field_data['new'])->fetchField();


      $outputArray[$type][$field]['legacy'] = $count ;
      $outputArray[$type][$field]['new'] = $count_new;     
    }
  }

答案 1 :(得分:0)

对于其他人的参考,如果您的settings.php中还没有第二个数据库,则可以这样做:

global $databases;

$seconddb = $databases['default']['default'];
$seconddb['database'] = 'seconddb';

Database::addConnectionInfo('seconddb', 'default', $seconddb);
db_set_active('seconddb');

// ...

db_set_active('default');