1

My Drupal 7 site needs to have articles, faq, pages, blog (not confirmed) and forum. But client can't store any data coming from front end. It can include the registrations from front end. That data need to be stored on external database and admin and users will access that data from the drupal site.

More specific, if user add comment or topic in forum, it should be saved in external database, and if admin adds a page from admin, it should be saved in local database. Moreover, even registration done from front end, needs to saved in external database

I suggested that we can use complete database externally which can be done easily but suggestion is rejected. Anyone can tell me any solution?

Note: the only solution I can think of is to create a custom module and use hooks for database functionality where I will check which content type is been called (means create, read, update and delete) and depending on that I will switch the database execute the query and return the data. I need to confirm which hooks I need to use.

  • Right now, the only solution i am able to think of is i will create custom module and use hooks for database functionality where i will check which content type is been called(means create, read, update and delete) and depend on that i will switch the database execute the query and return the data. I need to confirm which hooks i need to use. – pramod nanavare Sep 1 '17 at 10:45
0

Yes you can do that.

There are two method for configuring external database.

  1. Inside settings.php
  2. Inside Module directly.

1. Inside settings.php

$databases = array();
$databases['default']['default'] = array(
  // Drupal's default credentials here.
  // This is where the Drupal core will store its data.
);
$databases['my_other_db']['default'] = array(
  // Your secondary database's credentials here.
  // You will be able to explicitly connect to this database from your modules.
);

In your module:

// Use the secondary database we set up earlier
db_set_active('my_other_db');

// Run some queries, process some data
// ...

// Go back to the default database,
db_set_active();

2. Inside Module directly.

If your module will be alone in using the secondary database you can define the connection directly in your module:

  $other_database = array(
      'database' => 'databasename',
      'username' => 'username', // assuming this is necessary
      'password' => 'password', // assuming this is necessary
      'host' => 'localhost', // assumes localhost
      'driver' => 'mysql', // replace with your database driver
  );
  // replace 'YourDatabaseKey' with something that's unique to your module
  Database::addConnectionInfo('YourDatabaseKey', 'default', $other_database);
  db_set_active('YourDatabaseKey');

  // execute queries here

  db_set_active(); // without the paramater means set back to the default for the site
  drupal_set_message(t('The queries have been made.'));

Refer https://www.drupal.org/docs/7/creating-custom-modules/howtos/how-to-connect-to-multiple-databases-within-drupal

  • Yes, I read that. But my problem is forum module will not be created by me but its data need to saved on external database. On other hand, if admin is adding FAQ, it need to store on local database. – pramod nanavare Aug 31 '17 at 12:13
  • I am able to get the solution. But it need to be secure also. Please check the following links. link. I get this from another question link – pramod nanavare Sep 5 '17 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.