node_save: The way to save node to your Drupal Code without running into INSERT query?

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


I was trying to create a new node programatically by inserting information in node, node_revision tables and using "sequences" table to figure out what is the latest nid and vid. But, for some reason it was failing and then I found the saviour, "node_save", the easy one.

The following code saves your node without any bug, error or tension:

     $edit = array();
                $edit['type'] = 'daylife';
                // get the node type defaults
                $edit['uid'] = 1;
                $edit['name'] = 'bollywood';
                $edit['promote'] = 0;
                $edit['comment'] = 0;
                //$edit['revision'] = ;
                //$edit['format'] = FILTER_FORMAT_DEFAULT;
                $edit['status'] = 1;

                $edit['title'] = $article['headline'];
                $edit['body'] = $body_all;

                $edit['date'] = format_date(time(), 'custom', 'Y-m-d H:i:s O');

                node_invoke_nodeapi($edit, 'daylife');

                node_validate($edit);
                if ($errors = form_get_errors()) {
                    //print_r($errors);
                }

                $node = node_submit($edit);
                node_save($node);

 

 

                                     
1 point

thank you for the code information...

Anonymous's picture
Created by Anonymous

Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.