【问题标题】:Drupal node_save fails but gives no errorsDrupal node_save 失败但没有给出错误
【发布时间】:2011-07-05 20:06:47
【问题描述】:

我正在构建一个批处理导入器模块,该模块将从一个数据库中获取数据并在 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 个小时试图调试它,但仍然无法弄清楚。任何帮助将不胜感激...

【问题讨论】:

  • 好吧,似乎这样做并将 cck 字段包装在此函数中修复了它: function cck_val($val = 0, $key = 'value') { return array(array($key => $验证)); }
  • 不幸的是,Drupal 在表单 API 中做了很多节点验证。

标签: php drupal drupal-6


【解决方案1】:

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 参数将在加载节点之前重置缓存。贵,但这是我知道的唯一方法。

然后您必须检查节点->[vars] 是否相互匹配,如果它们都匹配则返回 TRUE。

我将上面的 node_save 代码示例归功于 http://www.learn-drupal.in/cck/how-to-create-a-drupal-node-programmatically.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2013-05-11
    相关资源
    最近更新 更多