我正在构建一个批处理导入程序模块,它将从一个数据库中获取数据并在drupal中创建节点。创建节点对象的代码是:
$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);
上面的内容在我的调试窗口http://screencast.com/t/R5PhWZWraIR8中输出 当我运行它时,它不会创建节点,但正如您从截屏视频中看到的那样,它将$ node-> validates设置为1,因此我认为它是有效的。我花了大约5个小时试图调试这个仍然无法搞清楚。任何帮助将不胜感激......
答案 0 :(得分:0)
Drupal 6示例:
<?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()不返回任何内容,因此不会检查。
您可以查看:
$new_node = node_load($node->nid, NULL, TRUE);
3d arg将在加载节点之前重置缓存。贵,但这是我所知道的唯一方式。
然后你必须互相检查node-&gt; [vars],如果它们全部匹配则返回TRUE。
我将http://www.learn-drupal.in/cck/how-to-create-a-drupal-node-programmatically.html归功于上面的node_save代码示例。