2

i want to create multiple nodes (from csv file) the csv is loaded well, and my table node contains many old records (with nid from 1 to 170) what i want is to record the csv's new nodes with nid strating from the last one on table node (the new nodes would start from 171 on nid) I've tried db_last_insert_id() with no success.

This is my db_merge part :

 $nodes = db_merge('node')
        ->key(array('nid' => last_insert_id())) 
        ->fields(array(
            'type' => 'livre',
            'uid' => 1,
            'status' => 1,
            'comment' => 1,
            'promote' => 0,
            'title' => $new_array['titre_csv']<===from csv
        ))
        ->execute();
  • 6
    Why don't you use node_save? Any particular reason? – Mołot Feb 8 '16 at 9:29
7

Try this way:

  $node = new stdClass();
  $node->title = $new_array['titre_csv'];
  $node->type = "livre";
  node_object_prepare($node); // Sets some defaults. Invokes hook_prepare() and hook_node_prepare().
  $node->language = LANGUAGE_NONE; // Or 'fr' in your case
  $node->uid = $user->uid; 
  $node->status = 1; //(1 or 0): published or not
  $node->promote = 0; //(1 or 0): promoted to front page
  $node->comment = 1; // 0 = comments disabled, 1 = read only, 2 = read/write
  $node = node_submit($node); // Prepare node for saving
  node_save($node);
  //save the other table

If you want to save it in your custom table you can do a db_insert or EntityFieldQuery.
In that case $node->nid value will be taken after you do the node_save (so do it after you save it with node_save).
You could also use a hook_schema for your table.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.