【发布时间】:2009-09-19 20:24:18
【问题描述】:
在 Drupal 中,如果你想在节点表中插入一些东西,'nid' 可以为 null,但 'vid' 不能。 node_revision 表的情况正好相反。
节点在创建时按什么顺序插入?
我无法插入节点表,因为我没有修订 ID;我无法插入到 node_revision 表中,因为我没有节点 ID。
另外,是否有某种功能可以轻松为您完成此插入操作?
【问题讨论】:
在 Drupal 中,如果你想在节点表中插入一些东西,'nid' 可以为 null,但 'vid' 不能。 node_revision 表的情况正好相反。
节点在创建时按什么顺序插入?
我无法插入节点表,因为我没有修订 ID;我无法插入到 node_revision 表中,因为我没有节点 ID。
另外,是否有某种功能可以轻松为您完成此插入操作?
【问题讨论】:
node_save function is available 的代码;你试过看看它是如何工作的吗?
这是与节点创建相关的部分:
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';
}
显然,它首先保存节点的修订,然后才保存节点本身。
之后,更新node_revisions记录,放入nid的值。
如果你不想保存一个节点,你不应该编写可以为你做这件事的代码:你只需要调用node_save,它就会保存节点,调用所需的钩子,等等。
【讨论】:
在 Drupal 7 中,与 Drupal 6 相比,顺序是颠倒的:首先保存节点,然后保存节点修订版。
// Save the node and node revision.
if ($node->is_new) {
// For new nodes, save new records for both the node itself and the node
// revision.
drupal_write_record('node', $node);
_node_save_revision($node, $user->uid);
$op = 'insert';
}
同样当节点更新时,先保存节点,再保存节点修订。
// For existing nodes, update the node record which matches the value of
// $node->nid.
drupal_write_record('node', $node, 'nid');
// Then, if a new node revision was requested, save a new record for
// that; otherwise, update the node revision record which matches the
// value of $node->vid.
if (!empty($node->revision)) {
_node_save_revision($node, $user->uid);
}
else {
_node_save_revision($node, $user->uid, 'vid');
$update_node = FALSE;
}
$op = 'update';
一旦节点修订被保存,节点行就会更新。
if ($update_node) {
db_update('node')
->fields(array('vid' => $node->vid))
->condition('nid', $node->nid)
->execute();
}
在Drupal 7中,节点表的vid字段可以是NULL,而nid和node_revision的vid字段都不是NULL,虽然nid的值默认为0。
【讨论】: