简体   繁体   中英

Drupal node_save fails but gives no errors

I'm building a batch importer module that will take data from one database and create the nodes in drupal. The code to create the node object is this:

$node = new stdClass();
$node->type = 'jobs';
$node->uid = 1;
$node->status = $row->J_Approved;
$node->title = $row->J_Title;
$node->comment = 0;
$node->revision = 1;
$node->promote = 0;
$node->sticky = 0;
$node->created = $row->J_DateTime_Mod;
$node->field_description = $row->J_Body;
$node->field_email = $row->J_MI_Email;
$node->field_jobs_fax = $row->J_MI_Phone;
$node->field_aia_firm = $row->J_AIA;
$node->field_name = $row->J_Sub_Name;
$node->field_phone = $row->J_Sub_Phone;
$node->field_jobs_email = $row->J_Sub_Email;
$node = node_submit($node);
node_save($node);

And the above outputs this in my debug window http://screencast.com/t/R5PhWZWraIR8 When I run this, it doesn't create the node, but as you can see from the screencast, it sets $node->validates to 1, so im assuming it is valid. I've spent around 5 hours trying to debug this and still can't figure it out. Any help would be appreciated...

Drupal 6 example:

<?php
$node = new stdClass(); //Create instance of class stdClass which will create node for you.
$node->type = 'library'; //Name of the content type
$node->field_book_no[0][value] = 22;//Book number CCK Field
$node->field_book_name[0][value] = "Drupal"; //Book name  CCK Field
$node->field_book_author[0][value]='Sachin'; //Author name -  CCK field
$node->field_year[0][value]='2011'; //Publication year - CCK field
$node->uid = 1; // user id, 1 is created by admin
$node->status = 1;//1 is published, 0 is unpublished
$node->promote = 0;//1 is promote to home page, 0 is not to promote on home page
node_save($node); // Save this node
?>

node_load() doesn't return anything, so it doesn't check.

You can check it with:

$new_node = node_load($node->nid, NULL, TRUE);

Where the 3d arg will reset the cache before loading the node. Expensive, but that's the only way I know.

You would then have to check the node->[vars] against each other, and return TRUE if they all match.

I credit http://www.learn-drupal.in/cck/how-to-create-a-drupal-node-programmatically.html with the node_save code example above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM