3

我一直在寻找如何在 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 );

当我执行此代码时,会创建节点,但是当我获得管理页面时,它会引发以下错误:

警告:第 258 行 C:\wamp\www\steelylib\includes\menu.inc 中为 foreach() 提供的参数无效。

警告:第 258 行 C:\wamp\www\steelylib\includes\menu.inc 中为 foreach() 提供的参数无效。

用户警告:键 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:\wamp\www\steelylib\sites \all\modules\nodecomment\nodecomment.module 在第 409 行。

警告:第 258 行 C:\wamp\www\steelylib\includes\menu.inc 中为 foreach() 提供的参数无效。

警告:第 258 行 C:\wamp\www\steelylib\includes\menu.inc 中为 foreach() 提供的参数无效。

我看过不同的教程,似乎都遵循相同的过程。我不确定我做错了什么。我正在使用 Drupal 6.15。当我回滚数据库时(在我进行更改之前),错误就消失了。

编辑
在玩了一下之后,我确实发现我的 hook_menu() 中的“访问参数”中有一个错误,但就重复条目而言,我永远无法弄清楚。

4

5 回答 5

3

我相信问题出在其他地方。上面的代码片段是 100% 正确的。但我相信你在某个地方有错误。

我在 menu.inc 的第 258 行遇到了警告。警告的来源是错误的菜单条目。检查模块中的所有hook_menu。一个常见的错误——比如我的——是给这些菜单项分配了错误的值:
、、、、'access callback''access arguments''page callback''page arguments'

请记住这些项目:

  • 'access arguments'并且'page arguments'必须是数组。
  • 如果您想授予对菜单条目的无限制访问权限,请执行以下操作:'access callback' => true

关于重复条目,我仍然不知道。

于 2010-03-31T08:54:35.587 回答
3

我在 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
}
于 2010-08-04T14:36:42.243 回答
1

您需要清除节点、节点修订和节点注释统计表。

问题是它试图插入一个已经存在于节点评论统计中的记录。

于 2010-03-30T23:20:23.453 回答
0

I'm not sure what's going on with your site exactly, would need check your db and other stuff, but the error you are seeing is cause by this line:

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);

It is exacuted when a node is saved and everything looks fine. It's the place where something get's inserted into that table. Somehow though, you already have an entry for the node with nid 36 in your node_comment_statistics table. I don't know if your tables are out of sync, or you are inserting two rows into this table.

Possible reasons:

  • You have some custom code / other module that uses this table?
  • You are triggering the nook_nodeapi op insert twice in your code when a node is created.
于 2010-03-31T13:46:40.653 回答
0

好的变化是这是一个权限问题。

就我而言,我必须将“故事:创建新内容”权限授予用户角色。

于 2014-09-15T10:15:24.137 回答