I'm doing a D2D migration that requires a mapping table to map tids from the source site to the destination site. In prepareRow(), I need to check the tid for a certain vocabulary and get the corresponding tid for the destination site. For a couple reasons, this mapping table is kept in the source site. However, the problem is that the default db connection that is being used in prepareRow() is the destination db, not the source db any more, so running my query throws an error. How can I switch the connection to use the source db so I can access this table and cache it? The short term method would be to copy this table to the destination site, but this will be an ongoing migration, and I don't want to have to update the table manually in the destination site every time the table is updated.


2 Answers 2


2 simple approaches:

You call the other DB explicitly like you do any other place to talk to it from drupal.

// Method #1
$query = Database::getConnection('default', 'for_migration')->select('source_table', 'st');
// $query = ....

// Method #2
// ... code here ...
db_set_active();  // switch back to normal DB connection in-use. This is important.

Method 1 is outlined here: https://www.drupal.org/node/1014558 Method 2 is outlined here: https://www.drupal.org/node/18429

Your other option is to not do it in every prepareRow() explicitly. Think outside the box :P. Why re-query this information within every prepareRow() -- that just slows down your migration. You can define this custom $tid_map lookup array in your Migration class's constructor as a data member to the migration class itself. You only query it's value a single time (on migration startup). prepareRow then just checks the stored array whenever needed. You may or may not have to use a static variable to assure the value is saved and preserved throughout the Migration lifetime ... I'm not sure.


What I ended up doing was to use method #1 as detailed by tenken above, with one addition.. I defined a separate connection in settings.php, and queried it directly in prepareRow(). However, I added caching to it (see here for instructions), so that the query isn't being run every time prepareRow() is run.

$target_channels = &drupal_static(__FUNCTION__);
if (!isset($target_channels)) {
  if ($cache = cache_get('nb_migrate_target_channels')) {
    $target_channels = $cache->data;
  // Not cached, so we need to generate it this time through.
  else {
    $map_query = Database::getConnection('default', 'mrctv')
      ->select('my_table', 'mt')
      ->fields('mt', array('source_channel', 'target_channel'))
      ->condition('mt.target_site', 'my_target_site');
    $map_result = $map_query->execute();
    foreach ($map_result as $map_record) {
      $target_channels[$map_record->source_channel] = $map_record->target_channel;
    // Cache the array.
    cache_set('nb_migrate_target_channels', $target_channels, 'cache');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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