public function DatabaseStorageControllerNG::save

Overrides DatabaseStorageController::save().

Added mapping from entities to storage records before saving.

Overrides DatabaseStorageController::save

1 call to DatabaseStorageControllerNG::save()
1 method overrides DatabaseStorageControllerNG::save()

File

drupal/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php, line 343
Contains \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 = $this->database
    ->startTransaction();
  try {

    // Ensure we are dealing with the actual entity.
    $entity = $entity
      ->getNGEntity();

    // Sync the changes made in the fields array to the internal values array.
    $entity
      ->updateOriginalValues();

    // 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);
    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);
      }
      if ($this->dataTable) {
        $this
          ->savePropertyData($entity);
      }
      $this
        ->resetCache(array(
        $entity
          ->id(),
      ));
      $this
        ->postSave($entity, TRUE);
      $this
        ->invokeHook('update', $entity);
    }
    else {
      $return = drupal_write_record($this->entityInfo['base_table'], $record);
      $entity->{$this->idKey}->value = $record->{$this->idKey};
      if ($this->revisionKey) {
        $record->{$this->revisionKey} = $this
          ->saveRevision($entity);
      }
      $entity->{$this->idKey}->value = $record->{$this->idKey};
      if ($this->dataTable) {
        $this
          ->savePropertyData($entity);
      }

      // Reset general caches, but keep caches specific to certain entities.
      $this
        ->resetCache(array());
      $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);
  }
}