1 entity.controller.inc public EntityDatabaseStorageController::save(EntityInterface $entity)

Implements EntityStorageControllerInterface::save().

Overrides EntityStorageControllerInterface::save

File

core/modules/entity/entity.controller.inc, line 637
Entity API controller classes and interface.

Class

EntityDatabaseStorageController
Implements the entity storage controller interface for the database.

Code

public function save(EntityInterface $entity) {
  $transaction = db_transaction();
  try {
    // Load the stored entity, if any.
    if (!$entity->isNew() && !isset($entity->original)) {
      $entity->original = entity_load_unchanged($this->entityType, $entity->id());
    }

    $this->preSave($entity);
    $this->invokeHook('presave', $entity);

    if (!$entity->isNew()) {
      $return = backdrop_write_record($this->entityInfo['base table'], $entity, $this->idKey);
      $this->resetCache(array($entity->{$this->idKey}));
      $this->postSave($entity, TRUE);
      $this->invokeHook('update', $entity);
    }
    else {
      $return = backdrop_write_record($this->entityInfo['base table'], $entity);
      // Reset general caches, but keep caches specific to certain entities.
      $this->resetCache(array());
      $this->postSave($entity, FALSE);
      $this->invokeHook('insert', $entity);
    }

    // Ignore slave server temporarily.
    db_ignore_slave();
    unset($entity->is_new);
    unset($entity->original);

    return $return;
  }
  catch (Exception $e) {
    $transaction->rollback();
    watchdog_exception($this->entityType, $e);
    throw new EntityStorageException($e->getMessage(), (int) $e->getCode(), $e);
  }
}