I've got a query which is causing a PHP error 500 whenever I add a left join:

PHP Fatal error: Call to a member function orderBy() on a non-object

Here's the query:

$query = db_select('my_table', 'mt')
         ->fields('u', array('name')
         ->leftJoin('users', 'u', 'u.uid = mt.user_id')
         ->orderBy('mt.my_table_id', 'DESC')

$result = $query->execute();

The query is to be used with a table + pager. If I remove the jeftJoin() and respective fields() the query works perfectly. Can anyone shed some light as to why this might be happening?

Many thanks

1 Answer 1


The documentation says:

Joins cannot be chained, so they have to be called separately (see Chaining). If you are chaining multiple functions together do it like this:

$query = db_select('node', 'n');
$query->join('field_data_body', 'b', 'n.nid = b.entity_id');
  ->fields('n', array('nid', 'title'))
  ->condition('n.type', 'page')
  ->condition('n.status', '1')
  ->orderBy('n.created', 'DESC')
  • Exactly what I was looking for - I didn't realise you can't chain joins. Many thanks for your help. Commented Sep 17, 2013 at 11:25
  • As a side note on this, the pager query must be added before the join: drupal.org/node/1298070#comment-5068720 Commented Sep 17, 2013 at 12:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.