Creating Nodes Programmatically in Drupal 6

I was looking for how to create nodes in Drupal 6. I found some entries here in stackoverflow, but the questions seemed to be for older versions, or the solutions did not work for me. So here is my current process for creating

$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 run this code, a node is created, but when I get the administration page, it throws the following errors:

warning: invalid argument provided by foreach () in C: \ wamp \ www \ steelylib \ includes \ menu.inc on line 258.

warning: invalid argument provided by foreach () in C: \ wamp \ www \ steelylib \ includes \ menu.inc on line 258.

user warning: Duplicate entry โ€œ36โ€ for requesting key 1: 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 provided by foreach () in C: \ wamp \ www \ steelylib \ includes \ menu.inc on line 258.

warning: invalid argument provided by foreach () in C: \ wamp \ www \ steelylib \ includes \ menu.inc on line 258.

I looked through various tutorials and everyone seems to follow the same process. I'm not sure what I'm doing wrong. I am using Drupal 6.15. When I roll back the database (right before making the changes), the errors disappeared. Any help is appreciated!

Edit:

After playing with it a bit, I found that I had an error in my "access arguments" in my hook_menu (), but as far as the record is repeated, I could never figure it out. So, if you run into the same problem, good luck and post here if you find the problem !:-)
+3
source share
5 answers

I believe the problem comes from another place. Code snippet above 100% correct. But I'm sure you have a mistake somewhere.

258 menu.inc. . hook_menu s .
, , : 'access callback', 'access arguments', 'page callback', 'page arguments'

:

  • 'access arguments' 'page arguments' .
  • , : 'access callback' => true

Duplicate, .

+3

node 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
}
+3

node, node node.

, , node.

+1

, , , , , :

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, . , - . , node nid 36 node_comment_statistics. , .

:

  • /โ€‹โ€‹ , ?
  • nook_nodeapi node.
0

- .

": " .

0

Source: https://habr.com/ru/post/1739207/


All Articles