In case you want to store user fields (or node fields. I haven't tested it but i'm sure it works) in a separate db, even on a separate server, this will work.
1. Define your remote db in settings.php -- this is documented elsewhere. Let's assume you call it 'foo_db'.
$databases['foo_db'] = array(....
2. Add the field(s) to user accounts through drupal user configuration. Add the appropriate tables and fields to the remote db. Let's say we have one field with name "testfield" of type text. Then add the table to remote called "testfield_table" with fields "uid" and "value."
3. Install the rules module.
4. Create a rule triggered on the event "Before saving a user account." Add an action to "Execute custom php code."
Put in code like this, that stores the value in the remote db:
db_set_active('foo_db');
$q1 = "delete from testfield_table where uid=" . $account->uid . ";"; //alternatively, do a query for a uid match and update or insert as necessary
db_query($q1);
$q2 = "insert into testfield_table (value, uid) values ('" . $account->field_testfield['und'][0]['value'] . "', " . $account->uid . ");";
db_query($q2);
db_set_active('default');
Now add another action, "Set a data value," and choose "account(saved user)" ... "account:field-testfield"
Under value, enter
<?php
echo '';
?>That handles writing to the remote and keeping sensitive data from the default db.
5. For reading, create a module called, say, foo. In foo.module, implement hook_user_load to grab the data from the remote rather than the default db. Thus:
function foo_user_load($users)
{
db_set_active('foo_db');
foreach($users as $k =>&$u)
{
$q = db_query("select * from testfield_table where uid=:d", array(':d' => $k));
$a = $q->fetchObject();
$u->field_testfield['und'][0]['value'] = $a->value;
unset($u->field_testfield['und'][0]['safe_value']);
}
db_set_active('default');
}
There you go. Note in #4 i delete pre-existing values before inserting, so that when i execute the query in #5, i will get at most one value. You'll have to adjust it for multivalue fields (ie, put more fields in the remote table and logic in the entry & read), but this furnishes a skeleton.
Comments
Thanks for sharing
I am working on a similar solution to integrate Drupal with other systems and I think this will help.
Regards
After some work on this, i
After some work on this, i settled on hook_entity_load, hook_entity_insert, and hook_entity_update to do the appropriate work. For nested field collections, the logic gets hairy but is doable. Also, i kept the "tree" information in the drupal db and put foreign db keys in the leaves of nested field collections in lieu of the actual values, which i stuck in the remote. I also kept a drupal var array containing each field collection's local tablename, the field containing the value in that table, the remote tablename, and the remote table value field. I was able to extract the entity names with some simple string manipulation as needed.