7

I am trying to write a query using db_select that selects a particular node by id, and then joins it's field_data_field_projectresources column and also selects the terms from taxonomy (because this is a term_reference field)

I have tried numerous attempts but I am finding myself stuck (I didn't even get to the part about joining the dr_taxonomy_term_data table)

 $query = db_select('node','n')
  ->condition('n.nid','5')
  ->fields('n')
  ->fields('p');
 $query->Join('field_data_field_projectresources','p','p.entity_id = n.nid');
 $result = $query->execute();

 while($record = $result->fetchAssoc()) {
   print_r($record);
 }

Any help appreciated, Thanks

  • Can use: '$result = db_query("SELECT dr_node.nid, dr_node.title as 'title', dr_taxonomy_term_data.name as 'resourceterm' FROM dr_node JOIN dr_field_data_field_projectresources ON dr_node.nid = dr_field_data_field_projectresources.entity_id JOIN dr_taxonomy_term_data ON dr_field_data_field_projectresources.field_projectresources_tid = dr_taxonomy_term_data.tid WHERE dr_node.nid = '5'"); ?> Resources for why make</br> <?php foreach ($result as $row) { print $row->resourceterm; ?><br/><?php }' – Andrew Welch Sep 1 '12 at 10:42
8

I hope this works for you:

 $query = db_select('node','n');
 $query->join('field_data_field_projectresources','p','p.entity_id = n.nid');
 $query->fields('n',array('yourfieldnames'))
       ->fields('p',array('yourfieldnames'))
       ->condition('n.nid','5');
 $result = $query->execute();

Here is another answer that will help you very much.

Check out documentation for 'join' on drupal.org.

  • 1
    The join method isn't chainable unfortunately, so it must be on it's own line. – rocketeerbkw Apr 22 '15 at 6:31

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.