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.
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 