3

I plan to list all polls in the most recent order where I need to join my "poll" table with the "node" table and display them with pager. Here is what I do:

$polls_query = db_select('poll', 'p')
              ->extend('PagerDefault')
              ->limit(3);
$polls_query->join('node', 'n', 'n.nid = p.nid')
            ->fields('p', array('nid'))
            ->condition('p.active', 1, '=')
            ->orderBy('n.created', 'DESC');
$result = $polls_query->execute();
foreach ($result AS $poll) {
  $polls[] = array('data' => drupal_render(node_view(node_load($poll->nid), 'teaser')));
}
$content['pager_list'] = array(
  '#theme' => 'item_list',
  '#items' => $polls,
  '#title' => t('Listing Consensus'),
  '#type' => 'ul',
);
$content['pager_pager'] = array('#theme' => 'pager');
return drupal_render($content);

Unfortunately I see a white screen of death and the php error log says "PHP Fatal error: Call to a member function fields() on a non-object in .. " and thats the line where I mention after fields. If I dont use join, everything works fine.

If anyone else has had similar problems, please share the solution.

2

The join() method doesn't return the query object to isn't chainable.

If you change your code to this you should be fine:

$polls_query = db_select('poll', 'p')
  ->extend('PagerDefault')
  ->limit(3);

$polls_query->join('node', 'n', 'n.nid = p.nid');

$polls_query->fields('p', array('nid'))
  ->condition('p.active', 1, '=')
  ->orderBy('n.created', 'DESC');

// etc...
| improve this answer | |
  • super! i actually resolved it myself and was about to post the solution.. but there you go, you have already done that.. many thanks! – Mukesh Agarwal Jul 13 '12 at 7:21

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.