hi there )

guys , I need to update the node body when NID of node is known

so I use node_save() function in hook_node_insert()
something like this =

function LL_node_insert($node) // use it because of needing NID of node
{
if (!isset($node->llcheck)) // check to stop recursion
  {
  
			$node->body['und']['0']['value'] =   "123"; // update body 
			
		 $node->llcheck =1; // up flag before node_save - stop recursion
		  node_save($node); // udate filed value  	     ERROR IS HERE
  } 
}

but i get error =

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1415' for key 'PRIMARY':

so -
1) I can't understand - why it's so ?
Where is unlogical action in my code - as I understand in hook_node_insert() the node was already saved in database - so I can update it using node_save() - but as we see there's error.

2) Please tell me - is there any better (that mine) way to stop recursion ?

big thanks for your future answers and approving silence too))

Comments

jaypan’s picture

You can insert your value before the node is even saved using hook_node_presave():

function mymodule_node_presave($node)
{
  $node->body[LANGUAGE_NONE][0] = 123;
}

This way the value will all be saved on the first run, and you don't need to worry about recursion.

Contact me to contract me for D7 -> D10/11 migrations.

vedro-compota’s picture

Jaypan , I'm glad to see you here)
but I can't use _node_presave because I need to know NID of the node.

vedro-compota’s picture

the best solution I find (thanks for russians brothers help) )) is to add $node->is_new = FALSE; before node_save($node);
so it'll be look like =

function LL_node_insert($node) // use it because of needing NID of node
{
if (!isset($node->llcheck)) // check to stop recursion
  {
            $node->is_new = FALSE;  // THIS IS IMPORTANT
            $node->body['und']['0']['value'] =   "123"; // update body
           
         $node->llcheck =1; // up flag before node_save - stop recursion
          node_save($node); // update filed value           NO ERROR NOW =))
  }
}
nlisgo’s picture

I couldn't get the above to work because I am using node access control quite extensively and I was receiving errors about duplicate entries in the node_access table.

<?php
function LL_node_insert($node) // use it because of needing NID of node
{
  $node->body[LANGUAGE_NONE]['0']['value'] =   "123"; // update body
  field_attach_update('node', $node);  // save the fields without saving the node table
}
?>
code-brighton’s picture

I can confirm that field_attach_update('node', $node); worked perfectly for me inside hook_node_insert. Thank you nlisgo!

dev.paulson’s picture

Perfect solution. It works perfectly for me, Thanks a lot, Nlisgo.

sch2’s picture

Really nice and clean solutions, much appreciated.

tejaspmehta’s picture

Any solution on this ? I am also stuck in similar situation and receiving duplicate error entry. My problem is that when i node is created i need node_id to perform some action based on node id for permission. hook_node_insert($node) provide node_id but node_save() is not completed while node_insert is running. So any solution for this ?