views:

75

answers:

1

In Drupal, if you want to insert something into the node table, 'nid' can be null, but 'vid' can't. The inverse is true for the node_revision table.

So my question is, in what order are inserts done to the Drupal db when a node is created?

i.e. I can't insert into the node table, because I don't have the VID, and I can't insert into the node_revision table, because I don't have a nid.

Also, is there some kind of function available that can easily do this insert for you?

+3  A: 

The code of the node_save function is available ; did you try to see how it's working ?

Here's the part that relates to node creation :

if ($node->is_new) {
  _node_save_revision($node, $user->uid);
  drupal_write_record('node', $node);
  db_query('UPDATE {node_revisions} SET nid = %d WHERE vid = %d', $node->nid, $node->vid);
  $op = 'insert';
}

Apparently, it first saves the revision of the node, and, only then, saves the node itself.

After that, the node_revisions record is updated, to put the value of the nid.


If you want t save a node, you shouldn't write code that would do that for you : you just have to call node_save, and it'll save the node, call the required hooks, and all that.

Pascal MARTIN
That was spot on. Thanks so much!
RD
You're welcome :-) (That's one nice thing I love about PHP and open-source : if you want to know how it works, you can always look at the source code ;-) ) ;; Have fun !
Pascal MARTIN