This is the first time I've tried to do this so I may be missing something really obvious. I'm developing a module which uses hook_node_insert() to catch the creation of a node, and puts the node into Drupal Queue for later treatment by a cron process.
At the end of the process, I want to update a field in the node and save it. I've tried using entity_metadata_wrapper as well as node_save, but in both cases I get an error "PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '3011' for key 'PRIMARY': INSERT INTO {node}....". Apparently, Drupal is unaware that the node exists already despite the fact that $node->nid is set (which is why we get a duplicate entry integrity constraint).
Looking at the node_save() code, it certainly seems to be supposed to check and load the node first if the nid is set (which it is - I've followed this step by step in debug). I must be doing something wrong, but what?
Here is the code from hook_node_insert which queues the node for treatment:
if ($node->type == "sms_message") {
$queue = DrupalQueue::get('sms_message');
$node_wrapper = entity_metadata_wrapper('node', $node);
$node_wrapper->field_queued->set(1);
drupal_set_message($node_wrapper->label() . ' has been queued');
$queue->createItem($node);
}
And here is the relevant code from the callback module for hook_cron_queue_info
function sms_textlocal_outgoing($node) {
global $user;
$user = user_load(1);
$node_wrapper = entity_metadata_wrapper('node', $node);
$message = $node_wrapper->field_sms->value();
$sms_sent = 0;
// Then we do some processing...
$node_wrapper->field_sms_sent->set($sms_sent);
node_save($node);
I've also tried:
$node_wrapper->save();
instead of node_save, but with the same result.