0

I read online that db_query is faster than db_select for complex queries. I wanted to check this for one of the queries I have written.

Following is the code with db_query:

$query = "SELECT og_users_roles.uid
        FROM {og_users_roles} AS og_users_roles
          LEFT JOIN {og_role} AS og_role
            ON og_users_roles.rid = og_role.rid
          LEFT JOIN {users} AS users
            ON og_users_roles.uid = users.uid
        WHERE og_users_roles.gid = :gid
          AND og_role.name =  :role_name
          AND users.uid <> 0
          AND users.status = 1";

$placeholders = array(
  ':gid' => $group_entity->nid,
  ':role_name' => OG_ADMINISTRATOR_ROLE,
);

$results = db_query($query, $placeholders)->fetchAll();

Now, before testing the performance I need to change above to user db_select.

This is what I have done:

$query = db_select('og_users_roles', 'o_u_r')
  ->leftJoin('og_role', 'o_r', 'o_u_r.rid = o_r.rid')
  ->leftJoin('user', 'u', 'o_u_r.uid = u.uid')
  ->fields('o_u_r', array('uid'))
  ->condition('o_u_r.gid', $group_entity->nid)
  ->condition('o_r.name', OG_ADMINISTRATOR_ROLE)
  ->condition('u.uid', 0, '<>')
  ->condition('u.status', 1);

$results = $query->execute()->fetchAll();

However, this seems to fail with a WSOD. Can someone point the error?

4

Give a try to Query coder module ( https://www.drupal.org/project/query_coder ): Convert SQL query to Drupal Database abstraction layer code.

enter image description here


Otherwise to debug the error turn on the error reporting, and check the error log which will point you to the suitable path.

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.