Hi folks,

I have created a custom contenttype with a node reference field. To modify this contenttype I have created a module with the nodeapi_hook using the 'insert'-operation. In this hook I modify the $node to my desires and then call node_save($node). The problem is, when the nodereferencefield is empyt and when I call node_save($node) inside the 'insert'-operation I'll get a duplicate unique-key constraint, because the there is already an entry in the relation table with $node->nid and NULL.

As a side question, is there a way to prevent that there is an entry in the relation table even though I dont have selected any relation? If I have an entry there with NULL, it messes up one of my views with an empty row.

hope I give you enough information to give me information ;)

greetings

Comments

lourenzo’s picture

As far as I know, noderefence uses form elements to validate data, so if you just set up an array of field values, like we can do with Text fields (and others), you'll get a NULL value after node_save.

The dirty hack I've used some times is to insert the values directly into the field table.
I know it's not the best and cleanest way to get it done, but just works.

--
Lourenzo Ferreira
http://lourenzo.blog.br

lefnire’s picture

Problem is, when you node_save($node) where $form_state['values']['whatever_field'][0]['nid']=NULL, you get all those "duplicate keys" errors anyway. So even if you want to do the insert in sql manually afterward, you're still going to get the errors after node_save()....

Any word in this space?

urbanfort’s picture

I've been working with this, inserting the reference records myself using db_query. Calling node_save($major_node) after inserting the rows has unexpected consequences. I'm currently looking in to ways to tell the node that it's been updated otherwise but not finding much in the way of documentation. I'd be happy to share what I've got thus far if anyone is interested.

One thing I'm particularly satisfied, I just look up the existing reference row by 'field_related_nid is NULL' with the appropriate id and perform an update if it's still there.

purabdk’s picture

$form_state['values']['whatever_field'][0]['nid']=123

Above code is working...nice.

light-blue’s picture