1

I'm on Drupal 6, and I have a multiple value CCK field. I need a PHP function to delete all the values in that field so I can later repopulate it. I am currently doing this:

if (isset($node->field_received_email[0]['value'])) {
  foreach($node->field_received_email as $key => $value) {
    $node->field_received_email[$key]['value'] = NULL;
  }
}

However, sometimes when I later repopulate it, I get Drupal dblog errors like:

Duplicate entry '16026-133' for key 'PRIMARY' query: INSERT INTO content_field_received_email (vid, nid, delta, field_received_email_value) VALUES (16026, 15888, 133, '[email protected]')

It appears that the primary key is remaining, and so when I go to write data again to the field it gives the duplicate primary key error.

Any suggestions on how to really delete all the values and keys from a multiple value CCK field in a given node so I can later re-write them?

Thanks a lot!

1 Answer 1

5

Try to unset item

if (isset($node->field_received_email[0]['value'])) {
  foreach($node->field_received_email as $key => $value) {
    unset($node->field_received_email[$key]);
  }
}
4
  • Thanks a lot! I'll give it a try. (Sorry I can't vote you up, this is my first post here.)
    – rahim123
    Commented May 3, 2013 at 14:26
  • Seems to work! I imagine that also unsets the value associated with that key too, right?
    – rahim123
    Commented May 3, 2013 at 14:33
  • yes, it's unset array item and value too because value is a child for it
    – smile
    Commented May 3, 2013 at 14:40
  • Great, it seems to be working just the way I wanted now. I really appreciate your help.
    – rahim123
    Commented May 3, 2013 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.