使用Drupal 7难以运行对辅助数据库的附带调用

I am in the process of doing a data migration from OpenCart into Drupal 7. I have my primary database which is setup specifically for Drupal 7. Drupal is installed. I have created a secondary database on the same server which is a copy of the OpenCart database that I will be migrating from.

I did this because there are a lot of overlapping tables and honestly I didn't really want to merge the two databases together since I will be dumping the OpenCart one just as soon as the products and related info is migrated over.

Anyway, I am finding that even though Drupal 7 easily supports this it seems to crap out the second you try to execute any queries on the secondary DB. By crap out I mean "white screen of death" crap out. If I enable the db query logging of the devel module then it outputs a few unformatted lines of that info on the WSOD.

Below is a sample of the code that I am using to do this. As you can see, even a simple select statement bombs entirely. Does anyone have any idea why this might be happening?

I would really like to make this work and do it with the migrate module. But I am about to throw my hands up and just write a custom script to connect to the database and output all of the data as a giant XML file and then hope that the migrate module can handle that (or import the aggregated data into a temp table at some point).

$query = Database::getConnection('opencart', 'opencart')->query("SELECT * FROM product");

if ($query != NULL) {
  $row = $query->execute()->fetchObject();
  echo "<pre>" . print_r($row, true) . "</pre>";
  echo "<pre>" . print_r($query, true) . "</pre>";
}
else {
  echo "Query is NULL.";
}

The way I've done this before is:

1- Edit your active settings.php file to include the secondary db in your $databases array. So you'd have something like

$databases = array(
  'default'=> array(...),
  'secondary' => array(...),
);

2- In your code, use db_set_active('secondary'); You can then execute db_query()s and possibly the likes of db_select(), etc.

3- Make sure to switch your active database back to the default as soon as possible. Watch out for function calls you make before you switch back to the default database. Many calls, like t(), require the default database, and will crap out it's not active.

I have finally figured this out with the help of a co-worker. The problem was a couple things. First when I was chaining the query() function onto Database::getConnection() I was then trying to run the execute() function. But the query() function doesn't support that. Instead I had to just run fetchAssoc() on the end of the $query var.

When I tried using the Database Extraction Layer to execute this complex query I was building it wrong due to what some of the functions return. The join and addExpression functions that I had been trying to use only return an alias, even as they modify the query object. I was trying to chain those inline, but you can't. So by instead calling those functions on the object one at a time on their own non-chained line I was able to get them to work. I was still able to chain together the fields and the groupby that I was using.

So to get it to work with the migrate module I just switched back to using the database abstraction layer and made the changes I mentioned above.

Thank you everyone for all of your helpful suggestions.