0

I'm writing a module to use on a multisite setup that enables nodes to be published to another site on the same installation.

Suppose I have Site A and Site B. I want this module to provide the ability to save a given node from Site A to Site B. Naturally, the way I have the database info set up in settings.php for Site A is as follows:

$databases['default']['default'] = array (
    'database'  => $db_name,
    'username'  => $db_user,
    'password'  => $db_pass,
    'host'      => 'localhost',
    'port'      => '',
    'driver'    => 'mysql',
    'prefix'    => $db_tables,
);

$databases['site_b']['default'] = array (
    'database'  => 'site_b',
    'username'  => $db_user,
    'password'  => $db_pass,
    'host'      => 'localhost',
    'port'      => '',
    'driver'    => 'mysql',
    'prefix'    => '',
);

...thus giving Site A the info it needs to access Site B's database.

When I execute the following code in the module via the hook_node_insert and hook_node_update hooks:

// Switch to Site B database
db_set_active('site_b');

// Save the node on Site B
node_save($node);

...it attempts to save the node on the current database instead, resulting in an SQL error (duplicate node ID). (Also, as a side note, I'm utilising a boolean property on the $node object to prevent the hooks and node_save() from throwing each other into an endless loop.)

I tried using drupal_get_schema(NULL, TRUE); directly after db_set_active('site_b'), but that made no difference. I've also cleared the cache in between every code change and test. No luck.

I don't know what's preventing it from working... maybe my $databases array is structured incorrectly?

4
  • I've tried to do the same exact thing in D6, with the same result. After much testing, I came to the conclusion that hooks only work for the local site's DB. I also couldn't pull data using the variable_get() method either... you might want to try to see if you can get ANY hooks to work on the remote DB.
    – Michael D
    May 21 '12 at 3:37
  • Mine is working now (almost) - I can't quite pinpoint which change enabled it to switch database, but at a guess it's the "prefix" array, which I populated appropriately (pointing 'default' to 'site_b.'). I now face an SQL error every time I add a new node - e.g. Duplicate entry '242-0-all' for key 'PRIMARY': INSERT INTO {node_access}. Seems to be a conflict with the node ID, which is odd because it generates a new one...
    – Riari
    May 21 '12 at 18:26
  • OK, it turns out that my code wasn't properly updating $node->nid and $node->vid based on which site's tables were being manipulated at any given time - so basically Site A was receiving node IDs used by Site B. I think this is why it looked like it wasn't switching DBs. I added in the necessary context-aware handling of the two properties and it seems to be fine now. P.S. I didn't add this as an answer as it isn't really an answer to my original question as such, but it solved a problem further down the line. :)
    – Riari
    May 22 '12 at 21:44
  • i am having the same exact problem, any solutions?
    – J.H.
    Mar 21 '13 at 22:26
1

Here is some help:

The general format for the $databases array is as follows:
 * @code
 * $databases['default']['default'] = $info_array;
 * $databases['default']['slave'][] = $info_array;
 * $databases['default']['slave'][] = $info_array;
 * $databases['extra']['default'] = $info_array;
 * @endcode
 *
 * In the above example, $info_array is an array of settings described above.
 * The first line sets a "default" database that has one master database
 * (the second level default).  The second and third lines create an array
 * of potential slave databases.  Drupal will select one at random for a given
 * request as needed.  The fourth line creates a new database with a name of
 * "extra".

Are you sure Drupal can connect to the second database? Check your login credential, put some dummy table and do a db_query to verify they exist only in the second one.

3
  • It can definitely connect. Both databases are accessed with the same user, which has privileges on them both. I've been working at it some more since asking the question and something I've done has caused it to semi-work - it now switches database as expected, BUT when I add a node, somewhere along the line it tries to write values to {node_access} on the CURRENT database instead (and fails - duplicate values). Really weird.
    – Riari
    May 21 '12 at 18:13
  • (NB - see my comment on the question for more clarification)
    – Riari
    May 21 '12 at 18:29
  • This is due to the db_transaction() at the top of node_save() firing up the transaction on the default database, unfortunately, you need to hack or patch core here...
    – SebCorbin
    Aug 14 '13 at 12:20

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.