I have some nodes of a content type with following fields:
- title
- body
- image
I want to programmatically (!important) set the body field as empty (to make it like it was never set with any value).
This is what I have done:
$this_node = node_load($nid);
$this_node->body = array(); //to set it as an empty field
node_save($this_node);
After executing this, the node is not saved. Instead, in db_log following error is loged:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '104418'
for key 'PRIMARY': INSERT INTO {node} (nid, vid, type, language, title, uid, status, created,
changed, comment, promote, sticky, tnid, translate, uuid) VALUES
(*******placeholders & their values*******) in drupal_write_record()
(line 7170 of /var/www/rotary.local/docroot/includes/common.inc).
Is there someting I am missing? How I can set the body field as empty?
NOTE:
I don't want to use field_attach_update
for caching purposes.
$this_node->body[$this_node->language] = array();
$node
array shows$node->body
as an empty array. However, using the logic you suggested, it shows$node->body
as an array with$node->body[language]
as empty array. I am worried this may not be ideal. Could you please support your answer with something that can convince me that it is safe to do so.$this_node->body
as empty array gives the PDOException error, while saving$this_node->body[$this_node->language]
as empty array does not give the error