node_save

Versions
4.6
node_save($node)
4.7 – 6
node_save(&$node)
7
node_save(stdClass $node)

Save a node object into the database.

▾ 7 functions call node_save()

blogapi_blogger_edit_post in modules/blogapi.module
Blogging API callback. Modifies the specified blog node.
blogapi_blogger_new_post in modules/blogapi.module
Blogging API callback. Inserts a new blog post as a node.
blogap_mti_publish_post in modules/blogapi.module
Blogging API callback. Publishes the given node
node_revision_delete in modules/node.module
Delete the revision with specified revision number.
node_revision_rollback in modules/node.module
Roll back to the revision with the specified revision number.
node_submit in modules/node.module
Accepts a submission of new or changed node content.
queue_vote in modules/queue.module

Code

modules/node.module, line 399

<?php
function node_save($node) {
  // Fetch fields to save to node table:
  $fields = node_invoke_nodeapi($node, 'fields');

  // Serialize the revisions field:
  if ($node->revisions) {
    $node->revisions = serialize($node->revisions);
  }

  // Apply filters to some default node fields:
  if (empty($node->nid)) {
    // Insert a new node.

    // Set some required fields:
    if (!$node->created) {
      $node->created = time();
    }
    if (!$node->changed) {
      $node->changed = time();
    }
    $node->nid = db_next_id('{node}_nid');

    // Prepare the query:
    foreach ($node as $key => $value) {
      if (in_array((string) $key, $fields)) {
        $k[] = db_escape_string($key);
        $v[] = $value;
        $s[] = "'%s'";
      }
    }

    // Insert the node into the database:
    db_query("INSERT INTO {node} (". implode(", ", $k) .") VALUES(". implode(", ", $s) .")", $v);

    // Call the node specific callback (if any):
    node_invoke($node, 'insert');
    node_invoke_nodeapi($node, 'insert');
  }
  else {
    // Update an existing node.

    // Set some required fields:
    $node->changed = time();

    // Prepare the query:
    foreach ($node as $key => $value) {
      if (in_array($key, $fields)) {
        $q[] = db_escape_string($key) ." = '%s'";
        $v[] = $value;
      }
    }

    // Update the node in the database:
    db_query("UPDATE {node} SET ". implode(', ', $q) ." WHERE nid = '$node->nid'", $v);

    // Call the node specific callback (if any):
    node_invoke($node, 'update');
    node_invoke_nodeapi($node, 'update');
  }

  // Clear the cache so an anonymous poster can see the node being added or updated.
  cache_clear_all();

  // Return the node ID:
  return $node->nid;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.