node_save in Drupal has issues while storing the values to MySQL database

Viewed 191

I got an issue in node_save in Drupal.

Below is my code:

function retrax_notify_create(){

    $nodeId = array();  
    $nodeObj = new stdClass(); // We create a new node object
    $nodeObj->type = "re_trax_comments_and_notes"; // Or any other content type you want
    $nodeObj->title = "Re-Trax Comments";
    $nodeObj->language = LANGUAGE_NONE; 
    node_object_prepare($nodeObj); // Set some default values.


    $nodeObj->uid = 474; 

        $nodeObj->field_userid_ref['und'][0]['value'] = '5';

        $nodeObj->field_siteid_ref['und'][0]['value'] = '6';

        $nodeObj->field_notify_count['und'][0]['value'] = 7;


    $nodeObj = node_submit($nodeObj); 
    node_save($nodeObj);
    $nodeId[nid] = $nodeObj->nid;

    echo '<pre>';
    print_r($nodeObj);
    return $nodeId;         

}

When I run the hook menu, default values like type, title, uid will be stored in node table and node id will also get generated.

But the values for other three tables will not get saved for

  • field_data_field_userid_ref
  • field_data_field_siteid_ref
  • field_data_field_notify_count

Database column of one of the tables is shown below

field_userid_ref_value  varchar(255)            Yes NULL    

All the content types are proper but still the value is not being saved to the respective tables.

What am I doing wrong?

1 Answers

I think that lines like:

$nodeObj->field_userid_ref['und'][0]['value'] = '5';

are wrong. You have parameter "value" only field type that really have a value, i.e. text field. If you have node reference or tag reference fields there should be something else. Probably:

$nodeObj->field_userid_ref['und'][0]['nid'] = '5';

for nodes, or

$nodeObj->field_userid_ref['und'][0]['tid'] = '5';

for tags, or

$nodeObj->field_userid_ref['und'][0]['uid'] = '5';

...for users, depending on what those fields are referencing...

Create some node the "common" way, from back-end, then print it and see what fields there exists.

Related