[英]Creating nodes programmatically in Drupal 6
我一直在寻找如何在 Drupal 6 中创建节点。我在 stackoverflow 上找到了一些条目,但问题似乎要么是针对旧版本,要么是解决方案对我不起作用。 好的,这是我当前尝试创建的过程
$node = new stdClass();
$node->title = "test title";
$node->body = "test body";
$node->type= "story";
$node->created = time();
$node->changed = $node->created;
$node->status = 1;
$node->promote = 1;
$node->sticky = 0;
$node->format = 1;
$node->uid = 1;
node_save( $node );
当我执行此代码时,节点已创建,但是当我获得管理页面时,它会引发以下错误:
警告:为 C:\\wamp\\www\\steelylib\\includes\\menu.inc 中的 foreach() 提供的参数无效,第 258 行。
警告:为 C:\\wamp\\www\\steelylib\\includes\\menu.inc 中的 foreach() 提供的参数无效,第 258 行。
用户警告:关键 1 查询的重复条目“36”:INSERT INTO node_comment_statistics (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (36, 1269980590, NULL, 1, 0) in C:\\stelylib\\www \\all\\modules\\nodecomment\\nodecomment.module 第 409 行。
警告:为 C:\\wamp\\www\\steelylib\\includes\\menu.inc 中的 foreach() 提供的参数无效,第 258 行。
警告:为 C:\\wamp\\www\\steelylib\\includes\\menu.inc 中的 foreach() 提供的参数无效,第 258 行。
我看过不同的教程,似乎都遵循相同的过程。 我不确定我做错了什么。 我正在使用 Drupal 6.15。 当我回滚数据库(在我进行更改之前)时,错误消失了。
编辑:
稍微玩了一下之后,我确实发现我的 hook_menu() 中的“访问参数”有错误,但就重复条目而言,我永远无法弄清楚。
我相信问题源于其他地方。 上面的代码片段是 100% 正确的。 但我相信你在某个地方有错误。
我在 menu.inc 的第 258 行遇到警告。 警告的来源是错误的菜单条目。 检查模块中的所有hook_menu 。
一个常见的错误 - 就像我的 - 是为这些菜单条目分配错误的值: 'access callback'
, 'access arguments'
, 'page callback'
, 'page arguments'
请记住这些项目:
'access arguments'
和'page arguments'
必须是数组。'access callback' => true
关于重复条目,我仍然不知道。
我在 Drupal 6 中以编程方式创建节点所做的是;
$node = new stdClass();
$node->name = "test title";
$node->title = $node->name;
$node->body = "test body";
$node->type = "story";
$node->created = time();
$node->changed = $node->created;
$node->status = 1;
$node->promote = 1;
$node->sticky = 0;
$node->format = 1;
$node->uid = 1;
if ($node = node_submit($node)) {
node_save($node);
}
else {
// Process error
}
您需要清除节点、节点修订和节点注释统计表。
问题是它试图插入节点评论统计中已经存在的记录。
我不确定你的网站到底发生了什么,需要检查你的数据库和其他东西,但你看到的错误是由这一行引起的:
db_query('INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (%d, %d, NULL, %d, %d)', $node->nid, $node->created, $node->uid, 0);
当一个节点被保存并且一切看起来都很好时,它就会被执行。 这是将某些东西插入该表的地方。 但是,不知何故,您的 node_comment_statistics 表中已经有了 nid 36 节点的条目。 我不知道您的表是否不同步,或者您正在向该表中插入两行。
可能的原因:
好的变化是这是一个权限问题。
就我而言,我必须向用户角色授予“故事:创建新内容”权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.