I'm currently migrating a custom csv - import module from D6 to D7. But I'm having problems with programmatically creating a node with a references field attached to it. When saving the node, I get the following exception:
PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'nid' for column 'delta' at row 1: INSERT INTO {field_data_field_nodereference} (entity_type, entity_id, revision_id, bundle, delta, language, field_parentregion_nid) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6); Array ( [:db_insert_placeholder_0] => node [:db_insert_placeholder_1] => 25 [:db_insert_placeholder_2] => 25 [:db_insert_placeholder_3] => someCustomType [:db_insert_placeholder_4] => nid [:db_insert_placeholder_5] => de [:db_insert_placeholder_6] => ) in field_sql_storage_field_storage_write() (line 425 of ... field_sql_storage.module).
$node = new stdClass ();
$node->type = 'someCustomType'; //has a references field called 'nodereference'
node_object_prepare($node);
$node->title = 'Title';
$node->body = 'Some content...';
$node->field_nodereference['de'][0]['nid'] = 25; //reference to node with nid 25
node_submit($node);
node_save($node); //exception thrown
I would be very thankful if someone could point out a way on how to programmatically adding a references field to a node. I have the feeling that I miss some fundamental aspects of creating the D7 fields programmatically.
Your help is appreciated!
Comments
Comment #1
fgmIn D7, the node body is no longer a hardcoded string/format pair but a field too: you can probably not get a correct node creation using $node->body = 'Some content';
Comment #2
danielb CreditAttribution: danielb commentedSomething like this should work.
I generated similar code with Node export, and it imported back fine.
In future, you could just var_dump a $node and see what it looks like.
Comment #3
geerlingguy CreditAttribution: geerlingguy commentedI'd like to know if there's some sort of helper function that, instead of creating an entire node with a reference, will allow me to add a reference to a node that's already created. I know I could do all this manually, but it would be awesome if there were something like
references_add_node_reference($nid, $nid_to_refer)
or something like that.Comment #4
danielb CreditAttribution: danielb commentedYou would also need to add a parameter to specify which field to add the reference to, which also means you'd have to have created the node reference field on the content type. In the end it would just be a case of using node_load(), modifying the node, and then doing a node_save(). The only issue I'm fuzzy on is identifying the node's available languages to add it in the right spot in the data structure.
Comment #5
Umayal CreditAttribution: Umayal commentedNode 1 has city value and node 2 refer node 1 city value.
when node 2 have the automatic value of node reference field passing nid of node1?
How is it possible ? pls help me?
Comment #6
nevets CreditAttribution: nevets commentedFor the city node change
to
That way you can use $city_node->nid to initialize the reference field in node 2.
Not sure what that for loop is about though, you are actually creating 4 nodes.
And I do not think calling node_object_prepare() after saving the node has any value.
Comment #7
Umayal CreditAttribution: Umayal commentedThanks for replay.
I change the city node as you said.
It so an error ---> Parse error: syntax error, unexpected T_VARIABLE
($city_node = node_submit($node);)
Comment #8
nevets CreditAttribution: nevets commentedWhats the complete code look like?
Comment #9
Umayal CreditAttribution: Umayal commentedComplete code
Comment #10
nevets CreditAttribution: nevets commentedI copies the code, saved as a file and did a syntax check and I do not get that parse error. One possibility that suggests is a hidden character that did not come over with the cut and paste.
Comment #11
Umayal CreditAttribution: Umayal commentedI also copy the code
Five content are created.
In inst(node2) content node reference field(node1) not selected default.
toneity,excelcity are in node reference field but not as selected one.
($node->field_nodereference['field_fnr_city']['und'][0]['nid'] = $city_node->nid;)
that is:
SSM in toneity city
KSR in excelcity city
how to pass city nid to inst node reference field.(through code manually)
Comment #12
Vj CreditAttribution: Vj commentedit will give you
SSM in toneity city
KSR in excelcity city
and Kongu with empty city field