如何使用node_save创建节点?


9

我正在尝试将当前的html网站迁移到Drupal。我必须迁移超过80,000页,因此我想创建一个模块,而不是坐在计算机前50年。我能够创建一个从每个目录提取html的脚本,现在我到达了需要创建节点的路障。我正在尝试使用创建一个新节点node_save(),但是当执行node_save时,PDOException我尝试的所有内容都会出现错误。我传入$node,这是一个数组,然后将其转换为对象。

PDOException:在field_sql_storage_field_storage_write()中(/srv/www/htdocs/modules/field/modules/field_sql_storage/field_sql_storage.module的第424行)。

这是我们当前创建节点的方式,但是会产生错误:

$node= array(
    'uid' => $user->uid,
    'name' => $user->name,
    'type' => 'page',
    'language' => LANGUAGE_NONE,
    'title' => $html['title'],
    'status' => 1,
    'promote' => 0,
    'sticky' => 0,
    'created' => (int)REQUEST_TIME,
    'revision' => 0,
    'comment' => '1',
    'menu' => array(
        'enabled' => 0,
        'mlid' => 0,
        'module' => 'menu',
        'hidden' => 0,
        'has_children' => 0,
        'customized' => 0,
        'options' => array(),
        'expanded' => 0,
        'parent_depth_limit' => 8,
        'link_title' => '',
        'description' => '',
        'parent' => 'main-menu:0',
        'weight' => '0',
        'plid' => '0',
        'menu_name' => 'main-menu',
    ),
    'path' => array(
        'alias' => '',
        'pid' => null,
        'source' => null,
        'language' => LANGUAGE_NONE,
        'pathauto' => 1,
    ),
    'nid' => null,
    'vid' => null,
    'changed' => '',
    'additional_settings__active_tab' => 'edit-menu',
    'log' => '',
    'date' => '',
    'submit' => 'Save',
    'preview' => 'Preview',
    'private' => 0,
    'op' => 'Save',
    'body' => array(LANGUAGE_NONE => array(
        array(
            'value' => $html['html'],
            'summary' => $link,
            'format' => 'full_html',
        ),
    )),
        'validated' => true,
);

node_save((object)$node);

// Small hack to link revisions to our test user.
db_update('node_revision')
    ->fields(array('uid' => $node->uid))
    ->condition('vid', $node->vid)
    ->execute();

Answers:


6

我认为您应该阅读Drupal 7中的如何以编程方式创建节点,注释和分类法

$node = new stdClass(); // We create a new node object
$node->type = "page"; // Or any other content type you want
$node->title = "Your title goes jere";
$node->language = LANGUAGE_NONE; // Or any language code if Locale module is enabled. More on this below *
$node->path = array('alias' => 'your node path'); // Setting a node path
node_object_prepare($node); // Set some default values.
$node->uid = 1; // Or any id you wish

// Let's add standard body field
$node->body[$node->language][0]['value'] = 'This is a body text';
$node->body[$node->language][0]['summary'] = 'Here goes a summary';
$node->body[$node->language][0]['format'] = 'filtered_html'; // If field has a format, you need to define it. Here we define a default filtered_html format for a body field

$node = node_submit($node); // Prepare node for a submit
node_save($node); // After this call we'll get a nid

为什么要下票?
vfclists 2014年

6

stdClass您可以尝试创建一个新stdClass()对象,然后使用node_object_prepare()准备用于创建新节点的对象,然后最终手动更改uid,名称,标题,语言,正文等的值,而不是将数组强制转换为对象此外,在将新节点保存到数据库之前,请确保使用node_submit()。

示例:http//drupal.org/node/1173136


1

您的问题是,您正在尝试使用nid = null和vid = null创建一个新节点,当您尝试插入索引号为0的新记录时,这会弄乱节点表-这会产生重复条目问题,并且令人困惑的drupal核心。顺便说一句-drupal核心易受诸如node_save之类的操作的影响而无法看到问题,并尝试将该记录插入db中-这会导致sql错误-并抛出PDO异常

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.