在 Drupal 6 中以编程方式创建节点

发布于 2024-08-27 12:01:09 字数 1268 浏览 4 评论 0原文

我一直在寻找如何在 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() 中的“访问参数”有错误,但就重复条目而言,我始终无法弄清楚。

I have been searching for how to create nodes in Drupal 6. I found some entries here on stackoverflow, but the questions seemed to either be for older versions or the solutions did not work for me. Ok, so here is my current process for trying to create

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

When I execute this code, the node is created, but when I got the administration page, it throws the following errors:

warning: Invalid argument supplied for foreach() in C:\wamp\www\steelylib\includes\menu.inc on line 258.

warning: Invalid argument supplied for foreach() in C:\wamp\www\steelylib\includes\menu.inc on line 258.

user warning: Duplicate entry '36' for key 1 query: 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 on line 409.

warning: Invalid argument supplied for foreach() in C:\wamp\www\steelylib\includes\menu.inc on line 258.

warning: Invalid argument supplied for foreach() in C:\wamp\www\steelylib\includes\menu.inc on line 258.

I've looked at different tutorials, and all seem to follow the same process. I'm not sure what I am doing wrong. I am using Drupal 6.15. When I roll back the database (to right before I made the changes) the errors are gone.

Edit:
After playing around with it a bit, I did find that I had an error in my 'access arguments' in my hook_menu(), but as far as the duplicate entry goes, I was never able to figure it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

能怎样 2024-09-03 12:01:09

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

我在 menu.inc 的第 258 行遇到了警告。警告的来源是错误的菜单条目。检查模块中的所有 hook_menu
一个常见的错误(就像我的一样)是为这些菜单项分配了错误的值:“访问回调”“访问参数”“页面回调” >, '页面参数'

请记住以下几点:

  • '访问参数''页面参数' 必须是数组。
  • 如果您想授予对菜单项的无限制访问权限,请执行以下操作: 'accesscallback' => true

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

I believe that the problem stems from somewhere else. Code snippet above is 100% correct. But I am sure you have a mistake somewhere.

I have encountered warnings in line 258 of menu.inc. Origin of warning was wrong menu entries. check all hook_menus in your module.
One common mistake -like mine- is assigning wrong values to these menu entries: 'access callback', 'access arguments', 'page callback', 'page arguments'

Keep these items in mind:

  • 'access arguments' and 'page arguments' must be arrays.
  • If you want to grant unlimited access to a menu entry do like this: 'access callback' => true

Regarding the Duplicate entry, I still have no idea.

情域 2024-09-03 12:01:09

我在 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
}

What I have done to programatically create node in Drupal 6 is;

$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
}
掀纱窥君容 2024-09-03 12:01:09

需要清空节点、节点修订、节点评论统计表。

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

You need to wipe out the node, node revision, and node comment statistics table.

The problem is it is trying to insert a record that already exists in node comment statistics.

很酷不放纵 2024-09-03 12:01:09

我不确定您的网站究竟发生了什么,需要检查您的数据库和其他内容,但您看到的错误是由这一行引起的:

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 的节点条目。我不知道您的表是否不同步,或者您正在向该表中插入两行。

可能的原因:

  • 您有一些使用此表的自定义代码/其他模块?
  • 创建节点时,您将在代码中触发 nook_nodeapi 操作插入两次。

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.
戏剧牡丹亭 2024-09-03 12:01:09

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

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

Good change are this is a matter of Permissions.

In my case, I had to give the "Story : Create new content" permission to user role.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文