2

I'm trying to add a field to my query by using addField(). However, whenever I add this to the query I get a whitescreen and an apache error message on the line after. Any help in resolving this would be much appreciated.

Apache error log:

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

db_select query:

$query = db_select('z_revision', 't')
    ->fields('t', array('drupal_user_id'))
    ->addField('t', 'z', 'category')
    ->extend('PagerDefault') # error on this line
    ->extend('TableSort')->orderByHeader($header)
    ->limit(30)
    ->execute();

Ideally this would give me a two column table with headers 'drupal_user_id' and 'category' - all the rows in the category column having 'z' as a value.

3

addField returns the alias of the field that's added, not the query object. You just need to break your code up a bit:

$query = db_select('z_revision', 't')
  ->fields('t', array('drupal_user_id'));

$query->addField('t', 'z', 'category');

$result = $query->extend('PagerDefault')
  ->extend('TableSort')
  ->orderByHeader($header)
  ->limit(30)
  ->execute();
  • Thanks Clive. That cleared the error but now I get a new one. Is addField() the best way to create a totally new arbirary field in my query results? I'm getting a Column not found in field list error now. Hoping to do something similar to this: stackoverflow.com/questions/3769702/… – RobotEyes Jul 8 '15 at 15:50
  • The easiest way to debug is to cast the query object to a string before you execute it - that's give you the raw SQL, should make it a lot easier to spot what's going wrong. It's difficult to guess what you're looking for without knowing the structure of the tables – Clive Jul 8 '15 at 15:58
  • 2
    Thanks Clive - I managed add a column on the fly using addExpression() api.drupal.org/comment/33553#comment-33553 – RobotEyes Jul 8 '15 at 16:03

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.