Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9009311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:09:25+00:00 2026-06-16T02:09:25+00:00

I have to update some nodereference fields from custom code in myModule. This has

  • 0

I have to update some nodereference fields from custom code in myModule. This has to be done when a new node of a particular type is being inserted so I implemented hook_node_insert as follows:

function myModule_node_insert($node){
    $settings = _settings_forms_nodereference_fields($node->type);
    $field = $settings['field'];
    $language = field_language('node', $node, 'body');
    $node->{$field}[$language][0]['nid'] = 408;
    $node->{$field}[$language][1]['nid'] = 408;
    $node->{$field}[$language][2]['nid'] = 408;
    node_save($node);
}

However, each time I try to create a new node, I get the following error;

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘436’ for key ‘PRIMARY’: INSERT INTO {node} (nid, vid, type, language, title, uid, status, created, changed, comment, promote, sticky, tnid, translate) VALUES ….

I have discovered it is because of the node_save function but without it my changes are not committed.

The full array is as follows:

[field_event_form_s_] => Array
    (
        [und] => Array
            (
                [0] => Array
                    (
                        [nid] => 261
                    )

                [1] => Array
                    (
                        [nid] => 262
                    )

                [2] => Array
                    (
                        [nid] => 263
                    )
            )
    )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:09:28+00:00Added an answer on June 16, 2026 at 2:09 am

    I found the answer to my question immediately after posting at my next google search. I think it can be useful for other people, myself included in the future.

    The following code does the trick:

    function myModule_node_insert($node){
        $settings = _settings_forms_nodereference_fields($node->type);
        $field = $settings['field'];
        $language = field_language('node', $node, 'body');
        $node->{$field}[$language][0]['nid'] = 408;
        $node->{$field}[$language][1]['nid'] = 408;
        $node->{$field}[$language][2]['nid'] = 408;
        field_attach_update('node', $node);
    }
    

    Instead of node_save, the field itself needs to be updated. This guy does a better job of explaining whilst keeping it simple.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code which I am using to update some values
I'm trying to update some properties with KVC. The properties have been synthesized. This
UPDATE - I have fixed some mistakes in the code below and the images
QUESTION 1 I have to update some fields of a table of access according
Recently I have been trying to update some code to utilise the standard C++
I have a style template (below) that does not update some my custom control
I have a static code to update some select boxes. The original structure code:
in my app i have some NSOperation that update some core data element from
I have a GridView and I need update some data inserting HTML CODE; I
in my app i have some NSOperation that update some core data element from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.