Can't save data in hook_node_update() I have a node type with two field_collection fields. When I save the node I need to write the entity_id of the first field_collection into the secondo field_collection. This is my code:

function mymodule_node_update($node){
    $entity_id = $node-&gt;field_collection_1['und'][0]['value'];
    $entiry_array = entity_load('field_collection_item',array(entity_id));
    $entity = array_shift($entity_array);

    if (empty($entity-&gt;field_reference['und'][0]['value'])) {
        $entity-&gt;field_reference['und'][0]['value'] = $node-&gt;field_collection_2['und'][0]['value'];
        $entity-&gt;save();
    }
}


There are 2 problems there: 
1) in hook_node_update() data is not committed yet https://drupal.org/node/1677830
2) $entity->save() triggers again hook_node_update() (and in my opinion it shouldn't)

So at the end in $node->field_collection_1 I have the right id $node->field_collection_2 would have, but due to recursive call to hook_node_update() and the fact that in hook_node_update() data is not committed to database yet field_collection_2 entity and all changes made to the node vanish.

Can someone suggest me a possible solution?

Thank you
hook_node_presave($node)


  Act on a node being inserted or updated.
  
  This hook is invoked from node_save() before the node is saved to the
  database.
  Parameters
  
  $node: The node that is being inserted or updated.


function hook_node_presave($node) {

if ($node-&gt;type == 'your_node_type') {
// you can write your desired code here


}

}