What is the proper query to return an array of nid's of nodes that have a node reference field referanceing node 50 (example node number).

Comments

nevets’s picture

You could use the views module (if for no other reason to determine the query)

TheOldGuy223’s picture

Well I need to determine the query to run operations on each node, not to display them.
Are you saying that the views module will actually ouput the queary language that i need?

TheOldGuy223’s picture

wow! I had the option turned off by default, but once you enable it it does indeed show it! Thanks that is a huge help!

TheOldGuy223’s picture

here is the query and what the beggining of the code im trying to write, I know I have the fetch field operation wrong, it was copied from another snippet I was using.

How can I alter this so that the variable $numbers_list contains an array of Node ID's that I can loop through using a foreach loop.

The queary was created in views and outputs the correct nodes in views, in this example instead of outputting the nodes in views i just want the node ids in an array so I can loop through them.

Hint:(Im pretty positive that the answer is in modifying the code array(":title" => $title, ":type" => $type))->fetchField();

$refid =  $node->field_campaign[$node->language][0]['nid'];
$numbers_list = db_query("SELECT node.created AS node_created, node.nid AS nid
FROM 
{node} node
LEFT JOIN {field_data_field_campaign} field_data_field_campaign ON node.nid = field_data_field_campaign.entity_id AND (field_data_field_campaign.entity_type = 'node' AND field_data_field_campaign.deleted = '0')
WHERE (( (node.status = '1') AND (node.type IN  ('phone_number')) AND (field_data_field_campaign.field_campaign_nid IN  ('$refid')) ))
ORDER BY node_created DESC"array(":title" => $title, ":type" => $type))->fetchField();
nevets’s picture

I would use this approach

$refid =  $node->field_campaign[$node->language][0]['nid'];
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'phone_number')
->propertyCondition('status', 1)
->fieldCondition('field_campaign', 'nid', $refid, '=');

$result = $query->execute();
if (isset($result['node'])) {
  $nids = array_keys($result['node']);
  // You can now loop through $nids
}
TheOldGuy223’s picture

I assuming that I will be putting the foreach loop in the place where you put the comment, correct?

$result = $query->execute();
if (isset($result['node'])) {
  $nids = array_keys($result['node']);
  // You can now loop through $nids

 foreach ($nids as &$value) {
    //Do something for each $nids
  }
}

Interesting, Ive never seen a query written in that way, could you point me to the documentation that talks about it.

Thanks for the code also!

nevets’s picture

That comes from reading How to use EntityFieldQuery which is specific to entities. For more general queries there is db_select(). You can also read about the Database API

TheOldGuy223’s picture

Great! was I correct how i utilized the foreach? Im currently trying to debug my code (not working but im sure its part of the code i didnt publish in this question)

nevets’s picture

In general it looks correct though I would not use &$value since nid is just a value. You might trying adding right before your 'for' loop drupal_set_message("Nids:<pre>" . print_r($nids, TRUE) . '</pre>'); to make sure the query is returning something.