I have 2 content types: video and product. Each product contains a node reference to a video, and products can be linked to multiple videos. I'm writing a custom module to add in lists of these products and videos from an outside URL with JSON. Loading the content into Drupal isn't an issue.
What I want to do is check to see if the product exists, and if so then to add an additional node reference to the loaded video. The JSON spits out an array of videos, and there's a product array within each video array (so 1-10 products can be linked to one video). Each product has a specific product ID I'm passing in, and with that I'm able to single out the product node that needs to be updated. I load the product node, and then instead of adding an additional reference, the code I have is replacing the node reference. I've tried a handful of changes which either alter nothing or throw an error.
Here's the code:
$result = $efq->entityCondition('entity_type', 'node')
->fieldCondition('field_product_id', 'value', $prodID, '=')
->execute();
if (!empty($result['node'])) {
$nids = array_keys($result['node']);
$node = node_load($nids[0]);
$node->field_video['und'][0]['target_id'] = $node_id;
node_save($node);
}
else
{
Add product node
}