New in Drupal 7 ( Part 2)

Posted Jan 13, 2011 // 1 comments
Brad :

There are a lot of new things for developers to take advantage of in Drupal 7. These sometimes replace the functionality that was available in Drupal 6 with new functions or hooks, and sometimes implement new functionality.

Master/Slave Replication

In Drupal 6, support for Master/Slave database replication could only be implemented by using a distribution such as Pressflow, but Drupal 7 natively supports replication.

The Drupal 7 query system allows queries to have attributes that direct the target of a query. If the target was set to a slaver server and no such server is configured or the server is not available, Drupal will default to using the master database.

In order to take advantage of this new functionality, module developers should add parameters to their queries when they are sure that the queries are safe to always be read by slave servers rather than from the master server.

For example, the following line will allow the query to be split off to a slave server if one is available.

1
$query = db_select('node', 'n', array('target' => 'slave'));

Views has a slave server option as well. If you navigate to the edit page for a view in the administration interface, under Advanced Settings -> Query Settings there is an option to “Use Slave Server”. By default, this option is not checked. Checking it will ensure that the query for that particular view or display will be split off to a slave server.

Query Changes

There are new functions to use for each type of query as well. Developers can still use db_query, although the parameters have changed.

However, in order to write queries that other modules can alter, select queries should be made by using db_select, which creates a new select query object that can be manipulated using query_alter hooks.

$query = db_select('node', 'n', array('target' => 'slave'));
$query->innerJoin('field_data_field_taxonomy_term', 't', 'n.nid = t.entity_id');
$result = $query->fields('n', array('nid'))
  ->condition('n.status', 0, '<>')
  ->condition('t.field_taxonomy_term_tid', $tid)
  ->execute()
  ->fetchCol();

Functions such as inserts, updates and deletes can be done with similar functions.

db_insert('table')
  ->fields(array(
    'pid' => $pid,
    'rid' => $rid,
  ))
  ->execute();
     
db_update('table')
  ->fields(array(
    'name' => $name,
    'description' => $description,   
  ))
  ->condition('nid', $nid)
  ->execute();
     
db_delete('table')
  >condition('pid', $pid)
  ->execute();           

These functions should always be used instead of db_query for the INSERT, UPDATE and DELETE queries in order to let those functions take care of creating the correct syntax for the database the site is using.

For more information on the Drupal 7 query system, check out this link.

About Brad

Web Developer Brad Blake brings a wealth of expertise to our team and our clients whenever he creates software tools and websites on the LAMP platform. For more than five years, he has been using PHP to build cutting-edge technologies that ...

more >

Read Brad 's Blog

Comments

by Keith (not verified) on Mon, 01/17/2011 - 21:28

How if i want to make the whole system read from slave?

Hi,

I want to make the whole Drupal 7 read only from slave. And write to master.

I understood it can be implemented while writing new modules. But, can i do the same for the whole Drupal 7?

Please help and thanks

Keith

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <strong> <code> <p> <img> <ul> <ol> <li> <h2> <h3> <h4> <b> <u> <i>
  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.