Some of functions like EntityAPIController::load($id) can use named indentity instead of drupal internal id.
But some of thme don't: EntityAPIController::resetCache() accepts only drupal internal integer id.
On direct call programmer can check it out from the code, but I stuck on that problem while using entity_load_unchanged() trying to get 'orignal' entity from DB and always getting me static cache because I was giving entity indentity() as id parameter.

It's confusing when sometimes you can use $name of the entity (load() method) and sometimes not (resetCache() method).

I guess resetCache() must check for given id and clear needed cache as its done in ::load() function:

    // Only do something if loaded by names.
    if ($this->nameKey == $this->idKey || is_numeric(reset($ids))) {
      ....
    }
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

SlyK created an issue. See original summary.

SlyK’s picture

Status: Active » Needs review
FileSize
1.31 KB

Here is what works for me:

public function resetCache(array $ids = NULL) {
    $this->cacheComplete = FALSE;
    if (isset($ids)) {
      if ($this->nameKey == $this->idKey || is_numeric(reset($ids))) {
        $cache = &$this->entityCache;
        $is_name = false;
      } else {
        $cache = &$this->entityCacheByName;
        $is_name = true;
      }

      foreach (array_intersect_key($cache, array_flip($ids)) as $id => $entity) {
        if($is_name) {
          unset( $this->entityCache[$cache[$id]->{$this->idKey}] );
        } else {
          unset( $this->entityCacheByName[$cache[$id]->{$this->nameKey}] );
        }
        unset($cache[$id]);
      }
    }
    else {
      $this->entityCache = array();
      $this->entityCacheByName = array();
    }
  }

P.S. In patch there are no "&" so it's broken.

Status: Needs review » Needs work

The last submitted patch, 2: _entity_2610852_resetCacheByName_#2_D7-1.6.patch, failed testing.