How to programmatically create nodereferences
jmcclelland - May 30, 2007 - 15:57
| Project: | Content Construction Kit (CCK) |
| Version: | 4.7.x-1.2 |
| Component: | nodereference.module |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Jump to:
Description
The only documentation I could find on how to programmatically add a cck node that had a node reference was here:
Which describes a potential bug, but has given me some clues.
In any event, I'm having trouble saving the node reference using both approaches discussed in that issue.
I'm using this code:
$node = new StdClass();
$node->type = 'content_forum_schedule';
$node->status = 1;
$room_node = node_load($room_nid);
$block_node = node_load($block_nid);
$node->title = $room_node->title . "/" . $block_node->title;
// Here is one nodereference field. In the db the field name is field_event_room_space_nid
$node->field_event_room_space = array(
0 => array(
'nid' => $nid
)
);
// And here is a second nodereference field (trying a different approach)
$field_time_block['nids'][$block_nid] = $block_nid;
$node->field_time_block = $field_time_block;
node_object_prepare($node);
node_validate($node,$error);
$node = node_submit($node);
node_save($node);This code properly saves the node, however, both nodereference fields are set to 0.
Any suggestions on how to make this work are greatly appreciated!

#1
I had a similar problem
the node references were being created perfectly, until the submit op was called because this was called twice
// _content_widget_invoke('process form values', $node);
once its called the second time it unsets them ( sometimes the first was still left ?? ) but i commented that second proccess form values invoke and it seems to be working fine
#2
Hopefully you figured this out....