If it's possible, how can I go about extracting the database abstraction layer to use in a non-Drupal site?
2 Answers
Are you looking to use the Drupal database API exactly as it currently exists, or do you just like the idea of a PDO abstraction layer?
If you're really trying to break out of Drupal's bootstrap and code, then check out using PHP Data Objects (PDO):
- http://www.php.net/manual/en/book.pdo.php
- http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/
Now, if you just want to use Drupal's database API to work with a database outside of Drupal's database (but within a custom module or within a custom .php file that bootstraps the database), you can really easily do so by switching databases via db_set_active()
. You'd throw something like this in your settings.php file:
<?php
$databases = array();
$databases['default']['default'] = array(
// Drupal's credentials here...
);
$databases['my_other_db']['default'] = array(
// My other database credentials here...
);
?>
and then you'd activate it via:
<?php
db_set_active('my_other_db'); // Use my_other_db
// do stuff using Drupal 7's awesome database API
db_set_active(); // Go back to Drupal's db.
?>
I wrote a blog post that sums up how to bootstrap Drupal in a standalone PHP script to use certain layers, you may find this useful: -
-
I realize this answer is a few years old, but do you happen to still have the content of the blog post?– ianmac45Commented Mar 25, 2016 at 16:29
-
If you want to use the Drupal database code in a website, without bootstrapping Drupal, you need the following files:
- includes/database/database.inc
- includes/database/sqlite/database.inc
- includes/database/mysql/database.inc
- includes/database/pgsql/database.inc
The code in those files needs to be changed, as:
The code uses the DRUPAL_ROOT constant. You could remove the code using that constant, and replace it with code that automatically loads the include file containing the class. For example, the following code uses
DRUPAL_ROOT
because Drupal cannot use its code registry until there isn't a database connection active, as its code registry uses a database table.// We cannot rely on the registry yet, because the registry requires an // open database connection. $driver_class = 'DatabaseConnection_' . $driver; require_once DRUPAL_ROOT . '/includes/database/' . $driver . '/database.inc'; $new_connection = new $driver_class(self::$databaseInfo[$key][$target]); $new_connection->setTarget($target); $new_connection->setKey($key);
It could be rewritten as follows, if you register your own function with spl_autoload_register().
$driver_class = 'DatabaseConnection_' . $driver; $new_connection = new $driver_class(self::$databaseInfo[$key][$target]); $new_connection->setTarget($target); $new_connection->setKey($key);
The functions use the global
$databases
, which is normally defined in the settings.php file. You could replace that variable with a static property of the Database class.