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' => $node->nid,
'rgid' => '13',
'andor' => NULL,
'action' => 'hide',
'target_type' => 'component',
'target' => '22',
'weight' => '13',
'rules' => array(
'0' => array(
'nid' => $node->nid,
'rgid' => '13',
'rid' => '0',
'source_type' => 'component',
'source' => '21',
'operator' => 'equal',
'value' => '0',
),
),
);
$node->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?