When programmatically adding conditional element to webform, I get errors I'm trying to add a new conditional attribute to a bunch of webforms using a script.  There are already 12 conditions, and I need to add a 13th one.

My code looks like this in a basic script module which has $node available:

if(stuff_is_true){
 $new_component = array(
  'nid' =&gt; $node-&gt;nid,
  'rgid' =&gt; '13',
  'andor' =&gt; NULL,
  'action' =&gt; 'hide',
  'target_type' =&gt; 'component',
  'target' =&gt; '22',
  'weight' =&gt; '13',
  'rules' =&gt; array(
    '0' =&gt; array(
      'nid' =&gt; $node-&gt;nid,
      'rgid' =&gt; '13',
      'rid' =&gt; '0',
      'source_type' =&gt; 'component',
      'source' =&gt; '21',
      'operator' =&gt; 'equal',
      'value' =&gt; '0',
    ),
  ),
);

$node-&gt;webform['conditionals'][] = $new_component;

// Save the changes
node_save($node);


All of this works if put into a hook_node_view() (without doing a node_save obviously), so I know it's structured properly.

The problem comes during node_save($node) which throws an error like:

Duplicate entry '3033-0' for key 'PRIMARY': INSERT INTO {webform_conditional}...


(3033 is the nid, and the 0 is the rgid key).

Basically, what it's doing (I think) is trying to re-submit the whole $node->webform['conditionals'] array as NEW data, so existing components hit this "duplicate primary key" error (because they already exist).

How can I tell node_save to only ADD my new conditional item to the end of the existing array instead of trying to re-insert the whole thing?
It seems as if there's really no clean way to do this.  What I realized is that it was easier to just remove all existing conditions, and then re-add them all with the additional condition included.  So I started by setting:

$node-&gt;webform['conditionals'] = array(); //remove all conditions
node_save($node); //save the node, removing all old conditions
$node-&gt;webform['conditionals'] = $new_array_of_conditions; //add entire new array of condtions
node_save($node); //save the node with new conditions


This doesn't break anything on old nodes because no data or anything else is reliant upon conditions...so removing them doesn't hurt anything or lose any crucial data.