sisko
sisko

Reputation: 9900

Drupal database join query

I am trying to retrieve information from two database tables in the same DB query using the following code:

$query = db_select('webform_questionnaire_fieldset', 'wqf');
$query->join('webform_component', 'wc', 'wqf.cid = wc.cid');
$query->fields('wqf')
  ->fields('wc')
  ->condition('wqf.cid', $cid, '=')
  ->condition('wqf.nid', $nid, '=')
  ->condition('wqf.cid', 'wc.cid', '=')
  ->execute()
  ->fetchAll();

The output I get does not resemble the database data but looks more like the database meta data, as follows:

SelectQuery::__set_state(array(
   'fields' => 
  array (
  ),
   'expressions' => 
  array (
  ),
   'tables' => 
  array (
    'wqf' => 
    array (
      'join type' => NULL,
      'table' => 'webform_questionnaire_fieldset',
      'alias' => 'wqf',
      'condition' => NULL,
      'arguments' => 
      array (
      ),
      'all_fields' => true,
    ),
    'wc' => 
    array (
      'join type' => 'INNER',
      'table' => 'webform_component',
      'alias' => 'wc',
      'condition' => NULL,
      'arguments' => 
      array (
      ),
      'all_fields' => true,
    ),
  ),
   'order' => 
  array (
  ), ......

What am I doing wrong?

Upvotes: 4

Views: 3915

Answers (1)

Muhammad Reda
Muhammad Reda

Reputation: 27023

Add the condition to the join statement. Like on table1.id = table2.id, then add $result = $query->execute()->fetchAll();

$query = db_select('webform_questionnaire_fieldset', 'wqf');
$query->join('webform_component', 'wc', 'wqf.cid = wc.cid');
$query->fields('wqf')
    ->fields('wc')
    ->condition('wqf.cid', $cid, '=')
    ->condition('wqf.nid', $nid, '=')
    ->condition('wqf.cid', 'wc.cid', '=');
$result = $query->execute()->fetchAll();

Hope this helps... Muhammad.

Upvotes: 6

Related Questions