0

Problem is in the title basically, I'm trying to update a specific field in my node and I have to do it by using drupal_write_record rather than loading and modifying the node object itself, or by using node_save etc, but my changes aren't being reflected until I flush the caches for the site.

Is there something I need to do to have Drupal immediately aware of the changes?

Code:

$next_delta = db_query('SELECT MAX(delta) + 1 AS del FROM {field_data_my_field} WHERE entity_id = :nid', array(':nid' => $nid))->fetchField();
  $data = array(
    'entity_type' => 'node',
    'bundle' => $node->type,
    'deleted' => 0,
    'entity_id' => $node->nid,
    'revision_id' => $node->vid,
    'language' => LANGUAGE_NONE,
    'delta' => $next_delta,
    'field_my_data' => 1,
  );
  drupal_write_record('field_data_my_field', $data);
  drupal_write_record('field_revision_my_data', $data);

I've tried adding entity_get_controller('node')->resetCache(array($node->nid)); into the code but that's not doing it either.

1 Answer 1

1

When you write directly to the table (which drupal_write_record() does), it bypasses any APIs that are called. As such, you should really only use drupal_write_record() for tables you've created yourself, that don't have any APIs to be bypassed. In this case, you are better off changing the values in the $node, then calling node_save() on it. This will not bypass any APIs.

4
  • I can't though, I've got an AJAX call which is trying to update a field on my node, and there'll be a lot of spikes of usage in a very short space of time, I've tried it before when enough people try concurrently to add data (which is definitely likely to happen) it overwrites each others data, because each new entry hasn't had time to save the data, and so, people overwrite each others entry Commented Apr 23, 2018 at 16:14
  • field_attach_update is quicker than node_save, and should also clear the relevant field caches. You can use it to update a single field too. @Andrew Jaypan is right, you don’t want to bypass the API here, especially not for something as fundamental as entity fields. You can easily leave things in an inconsistent state and not realise until later when it’s too late to do anything about it. If you’re getting race conditions you should think about implementing a table locking strategy of some sort
    – Clive
    Commented Apr 23, 2018 at 16:50
  • @Clive I've just tried doing this, my problem is that when the node is loaded, I'm trying to just add a new record to my field, so I'm just adding the record like outlined in here (drupal.stackexchange.com/questions/64833/…) except I've just tested doing a concurrent submission (which is definitely going to happen) and it's only recording 1 extra result, it's not saving my 5 records, only 1. I'm assuming that's because each request loads the node, and because they each try to add 1 item and then save it, they're overwriting each other Commented Apr 23, 2018 at 17:05
  • Makes sense, logically speaking I think all you can really do is implement a lock in code and/or beef up your server resources
    – Clive
    Commented Apr 23, 2018 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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