-2

I have a little function that deletes specific entity references directly from database. Because of fact that it bypass the whole entity_load / entity_save system it flushes cache of all entities it affects by itself.

The weird thing is that it doesn't work. The references, while not present in the database, still show up on node view / edit pages.

  $affected_users = db_select('field_data_fmu_users_firm', 'su')
          ->fields('su', array('entity_id'))
          ->condition('fmu_users_firm_target_id', $user_id, '=')
          ->execute()->fetchCol();

  db_delete('field_data_fmu_users_firm')
      ->condition('fmu_users_firm_target_id', $user_id, '=')
      ->execute();

entity_get_controller('user')->resetCache($affected_users);

No special caching module is enabled on the site.

  • 3
    So why don't you delete them using API? Have you tracked what API is doing when saving them, to be sure you un-do it perfectly? First thought is "revisions", but it may be pretty many other things, too. – Mołot Apr 13 '14 at 17:53
  • Because it's way faster and I don't really need to care about other modules interaction (hooks, etc.). Considering revisions, it doesn't make much sense since I flush caches and edit the 'data' versions of field table (and not the revision table). – Petrroll Apr 13 '14 at 19:13
  • So... you are leaving reference in latest revision data, and you're surprised reference is still visible? Anyway, not using a CMS at all and coding everything directly is faster and more stable & predictable than circumventing CMS functionality. – Mołot Apr 13 '14 at 20:12
  • As far as I know the newest revision is always stored in (and retrieved from) the fielddata table not the revision table. – Petrroll Apr 13 '14 at 20:34
  • This question appears to be off-topic because it is about munging the database in a way that has unwanted side-effects and the solution offered by the OP is unlikely to help future readers. – Free Radical Apr 14 '14 at 2:25
2

Core has caches. Use the API. If you still do not want to use the API this might be helpful
Is it proper to perform a db_update on an entity table?

| improve this answer | |
  • I know that core has caches but those should be emptied after entity_get_controller('user')->resetCache($affected_users); – Petrroll Apr 13 '14 at 19:10
  • That will clear the static cache not the cache_field cache – mikeytown2 Apr 13 '14 at 19:46
  • That will be it. – Petrroll Apr 13 '14 at 20:32
0

As @mikeytown2 suggested the problem was in cache_field cache. The solution is quite simple:

// Clear field cache
cache_clear_all("field:node:$node->nid", 'cache_field');
| improve this answer | |

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.