1

I am trying to update node fields directly with db_update():

foreach ($_POST as $key => $_POST_value) {

  $num_updated = db_update('field_data_krown_weight')
  ->fields(array(
    'krown_weight_value' => $_POST_value['weight'],
  ))
  ->condition('entity_id', $_POST_value['nid1'], '=')
  ->execute();

}

It works pretty well, the data is well updated into database, and very fast. The problem is that when I go to the node form edit, the value in my field hasn't been updated.

I read that we must use field_attach_update() instead, something like:

foreach ($_POST as $key => $_POST_value) {

  $node1 = node_load($_POST_value['nid1']);
  $node1->krown_weight['und'][0]['value'] = $_POST_value['weight'];
  field_attach_update('node', $node1);

}

The problem is that with field_attach_update(), the query takes much more time to be executed! I need to update several nodes at the same time, so I would like to avoid field_attach_update()..

How can I perform node fields updates without processing all the node_save() stuff?

I also tried to reset the cache after db_update(), but this doesn't seems to work:

foreach ($_POST as $key => $_POST_value) {

  $num_updated = db_update('field_data_krown_weight')
  ->fields(array(
    'krown_weight_value' => $_POST_value['weight'],
  ))
  ->condition('entity_id', $_POST_value['nid1'], '=')
  ->execute();

  entity_get_controller('node')->resetCache(array($_POST_value['nid1']));

}

Your Answer

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

Browse other questions tagged or ask your own question.