2

I want to add an article to the db using db insert along with its field like tags and image.

A sample code i tried(below) is adding data to the db but I can't find any entry in the

db_insert('node')
  ->fields(array(
    'title' => "hello world",
    'created' => REQUEST_TIME,
    // 'body' => 'you did it!',
    'uid' => 1,
    'vid' => 5,
    'type'=>'article',

  ))
   ->execute();

If not this, can it be done using a normal sql query?

Why I want to do this way instead of nodesave?

  • I don't want any hooks working on this process.
  • This is a temperory fast solution I want for a project.
2

If you need to save a node without invoking any hook, then use the same code node_save() uses, excluding the part invoking hooks.

  $transaction = db_transaction();

  try {
    if (!empty($node->nid) && !isset($node->original)) {
      $node->original = entity_load_unchanged('node', $node->nid);
    }

    field_attach_presave('node', $node);
    global $user;

    if (!isset($node->is_new)) {
      $node->is_new = empty($node->nid);
    }

    if (empty($node->created)) {
      $node->created = REQUEST_TIME;
    }
    $node->changed = REQUEST_TIME;
    $node->timestamp = REQUEST_TIME;
    $update_node = TRUE;

    if ($node->is_new || !empty($node->revision)) {
      if (!isset($node->log)) {
        $node->log = '';
      }
    }
    elseif (!isset($node->log) || $node->log === '') {
      unset($node->log);
    }

    if (!$node->is_new && !empty($node->revision) && $node->vid) {
      $node->old_vid = $node->vid;
      unset($node->vid);
    }

    if ($node->is_new) {
      drupal_write_record('node', $node);
      _node_save_revision($node, $user->uid);
      $op = 'insert';
    }
    else {
      drupal_write_record('node', $node, 'nid');
      if (!empty($node->revision)) {
        _node_save_revision($node, $user->uid);
      }
      else {
        _node_save_revision($node, $user->uid, 'vid');
        $update_node = FALSE;
      }
      $op = 'update';
    }
    if ($update_node) {
      db_update('node')->fields(array('vid' => $node->vid))->condition('nid', $node->nid)->execute();
    }

    // Call the node specific callback (if any). This can be
    // node_invoke($node, 'insert') or
    // node_invoke($node, 'update').
    node_invoke($node, $op);

    // Save fields.
    $function = "field_attach_$op";
    $function('node', $node);

    $delete = $op == 'update';
    node_access_acquire_grants($node, $delete);

    unset($node->is_new);
    unset($node->original);
    entity_get_controller('node')->resetCache(array($node->nid));

    db_ignore_slave();
  }
  catch (Exception $e) {
    $transaction->rollback();
    watchdog_exception('node', $e);
    throw $e;
  }

I didn't remove the call to node_invoke(), since that calls the hooks implemented by the module implementing the content type, and those hooks are necessary for the content type to work correctly.
I also left the call to db_transaction() because, in the case one of the operations fails, all the operations are rolled back, and the database is left in a consistent state, which means (for example) that every table will refer to the same node version ID.

2
  • 1
    That really helped.
    – esafwan
    Sep 23 '13 at 5:18
  • This made bulk creation of nodes way faster, thank you.
    – W.M.
    Jul 22 '17 at 18:10
1

Although strongly recommended to use node_save instead (e.g via Batch API ), if you're going to manually add nodes that way you'll need to insert into the node_revision table as well.

2
  • 1
    He might also want to update the field_<field_name> and the instance tables of the fields which are a part of the node being added.
    – AjitS
    Sep 22 '13 at 11:43
  • Totally agree with @AjitS Sep 23 '13 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.