I want to query users, nodes etc in my D7 database that I am currently migrating to D8 to compare with the migrated content.

I want to use custom Drush commands to run some checks.

Is this possible?

  • 1
    No, not really. You can query data from as secondary database, Drupal or otherwise, but you won't be able to load D8 objects from a D7 database - the underlying architecture is incompatible. – Jaypan May 3 '19 at 4:19
  • Ah ok I thought that might be the case. Thanks. Great talk @DrupalSouth too btw ;) – dibs May 3 '19 at 4:22
  • Thanks! (post must be 15 characters) – Jaypan May 3 '19 at 5:08

You can add external databases in your settings.php file:

$databases['external']['default']= array(

Then, in a custom module you can query for nodes, terms etc.

// Switch to external database

// Get a connection going
$db = \Drupal\Core\Database\Database::getConnection();

$vid = 5; // The vocabulary I want to query
$query = $db->select('taxonomy_term_data', 't');
  ->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
  ->fields('h', array(
  ->condition('t.vid', $vid)
$results = $query->execute()->fetchAll();

Switch back to your current db


Save the results to a d8 vocabulary

foreach($results as $result) {
    'tid' => $result->tid,
    'name' => $result->name,
    'vid' => 'my_d8_vocabulary_name'
|improve this answer|||||

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.