开发者

Creating nodes programmatically in Drupal 6

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: Invali开发者_StackOverflow中文版d 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.


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.


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
}


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜