public function DatabaseStorageController::save

Implements Drupal\Core\Entity\EntityStorageControllerInterface::save().

Overrides EntityStorageControllerInterface::save

1 call to DatabaseStorageController::save()
2 methods override DatabaseStorageController::save()
DatabaseStorageControllerNG::save in drupal/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php
Overrides DatabaseStorageController::save().
UserStorageController::save in drupal/core/modules/user/lib/Drupal/user/UserStorageController.php
Overrides Drupal\Core\Entity\DatabaseStorageController::save().

File

drupal/core/lib/Drupal/Core/Entity/DatabaseStorageController.php, line 490
Definition of Drupal\Core\Entity\DatabaseStorageController.

Class

DatabaseStorageController
Defines a base entity controller 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);
    if (!$entity
      ->isNew()) {
      if ($entity
        ->isDefaultRevision()) {
        $return = drupal_write_record($this->entityInfo['base_table'], $entity, $this->idKey);
      }
      else {

        // @todo, should a different value be returned when saving an entity
        // with $isDefaultRevision = FALSE?
        $return = FALSE;
      }
      if ($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'], $entity);
      if ($this->revisionKey) {
        $this
          ->saveRevision($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);
  }
}