I'm not sure I'm understanding how to use Drupal with multiple database connections. I know I can setup more than one connection. It's not that.

I use SQL. I do not want to not use SQL for this part of Drupal (it is not part of the core of Drupal). It is a "report" almost, except it can link to other data in other databases. I have to query three-plus databases and bring back the data and have it display in the CMS like any other set of data someone might want.

For example, I do not wish to do this or this (from Drupal's documentation):

$result = db_select('example', 'e')
  ->fields('e', array('id', 'title', 'created'))
  ->condition('e.uid', $uid)
  ->orderBy('e.created', 'DESC')
  ->range(0, 10)

The queries are large and complicated. Am I fighting against the CMS/Framework, if I use [db_query][3]() everywhere for these outside databases that are unrelated to Drupal's core?

I can't find examples of anyone doing this, but I am still searching.

  • Nope Drupal won't fight you if you want to use raw queries. The query builder is essentially just for building the query string, you still have access to all of the same result methods with a raw query, plus parameter replacement in the original query, so all is well. The query method itself doesn't dictate which database is used, you switch beforehand with \Drupal\Core\Database\Database::setActiveConnection, then switch back when you're done. Is that what you needed to know? I wasn't sure if I'd understood the angle of the question right hence not an answer yet
    – Clive
    Apr 6, 2017 at 18:07
  • @Clive Yes that is what I needed. Thanks. What can I read on how I would display this data? I want to display it in the templated CMS just like any other data. Do I create a block for it?
    – johnny
    Apr 6, 2017 at 18:36


Your Answer

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

Browse other questions tagged or ask your own question.