public function DatabaseStorageControllerNG::save

Overrides DatabaseStorageController::save().

Added mapping from entities to storage records before saving.

Overrides DatabaseStorageController::save

File

drupal/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php, line 169
Definition of Drupal\Core\Entity\DatabaseStorageControllerNG.

Class

DatabaseStorageControllerNG
Implements Field API specific enhancements to the DatabaseStorageController class.

Namespace

Drupal\Core\Entity

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);

    // Create the storage record to be saved.
    $record = $this
      ->maptoStorageRecord($entity);

    // Update the original values so that the compatibility mode works with
    // the update values, what is required by field API attachers.
    // @todo Once field API has been converted to use the Field API, move
    // this after insert/update hooks.
    $entity
      ->updateOriginalValues();
    if (!$entity
      ->isNew()) {
      if ($entity
        ->isDefaultRevision()) {
        $return = drupal_write_record($this->entityInfo['base_table'], $record, $this->idKey);
      }
      else {

        // @todo, should a different value be returned when saving an entity
        // with $isDefaultRevision = FALSE?
        $return = FALSE;
      }
      if ($this->revisionKey) {
        $record->{$this->revisionKey} = $this
          ->saveRevision($entity);
      }
      $this
        ->resetCache(array(
        $entity
          ->id(),
      ));
      $this
        ->postSave($entity, TRUE);
      $this
        ->invokeHook('update', $entity);
    }
    else {
      $return = drupal_write_record($this->entityInfo['base_table'], $record);
      if ($this->revisionKey) {
        $entity->{$this->idKey}->value = $record->{$this->idKey};
        $record->{$this->revisionKey} = $this
          ->saveRevision($entity);
      }

      // Reset general caches, but keep caches specific to certain entities.
      $this
        ->resetCache(array());
      $entity->{$this->idKey}->value = $record->{$this->idKey};
      $entity
        ->enforceIsNew(FALSE);
      $this
        ->postSave($entity, FALSE);
      $this
        ->invokeHook('insert', $entity);
    }

    // Ignore slave server temporarily.
    db_ignore_slave();
    unset($entity->original);
    return $return;
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog_exception($this->entityType, $e);
    throw new EntityStorageException($e
      ->getMessage(), $e
      ->getCode(), $e);
  }
}