By vedro-compota on
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
You can insert your value
You can insert your value before the node is even saved using hook_node_presave():
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.
Jaypan , I'm glad to see you
Jaypan , I'm glad to see you here)
but I can't use _node_presave because I need to know NID of the node.
the best solution I find
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 =
My solution
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.
I can confirm that field
I can confirm that field_attach_update('node', $node); worked perfectly for me inside hook_node_insert. Thank you nlisgo!
Perfect solution
Perfect solution. It works perfectly for me, Thanks a lot, Nlisgo.
Really nice and clean
Really nice and clean solutions, much appreciated.
Any solution on this ? I am
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 ?
Have you tried the solution
Have you tried the solution that I suggested?