You are here

public function EntityAPIController::save in Entity API 7

Implements EntityAPIControllerInterface.

Parameters

$transaction: Optionally a DatabaseTransaction object to use. Allows overrides to pass in their transaction object.

Overrides EntityAPIControllerInterface::save

1 call to EntityAPIController::save()
EntityAPIControllerExportable::save in includes/entity.controller.inc
Overridden to care exportables that are overridden.
1 method overrides EntityAPIController::save()
EntityAPIControllerExportable::save in includes/entity.controller.inc
Overridden to care exportables that are overridden.

File

includes/entity.controller.inc, line 440
Provides a controller building upon the core controller but providing more features like full CRUD functionality.

Class

EntityAPIController
A controller implementing EntityAPIControllerInterface for the database.

Code

public function save($entity, DatabaseTransaction $transaction = NULL) {
  $transaction = isset($transaction) ? $transaction : db_transaction();
  try {

    // Load the stored entity, if any.
    if (!empty($entity->{$this->idKey}) && !isset($entity->original)) {

      // In order to properly work in case of name changes, load the original
      // entity using the id key if it is available.
      $entity->original = entity_load_unchanged($this->entityType, $entity->{$this->idKey});
    }
    $entity->is_new = !empty($entity->is_new) || empty($entity->{$this->idKey});
    $this
      ->invoke('presave', $entity);
    if ($entity->is_new) {
      $return = drupal_write_record($this->entityInfo['base table'], $entity);
      if ($this->revisionKey) {
        $this
          ->saveRevision($entity);
      }
      $this
        ->invoke('insert', $entity);
    }
    else {

      // Update the base table if the entity doesn't have revisions or
      // we are updating the default revision.
      if (!$this->revisionKey || !empty($entity->{$this->defaultRevisionKey})) {
        $return = drupal_write_record($this->entityInfo['base table'], $entity, $this->idKey);
      }
      if ($this->revisionKey) {
        $return = $this
          ->saveRevision($entity);
      }
      $this
        ->resetCache(array(
        $entity->{$this->idKey},
      ));
      $this
        ->invoke('update', $entity);

      // Field API always saves as default revision, so if the revision saved
      // is not default we have to restore the field values of the default
      // revision now by invoking field_attach_update() once again.
      if ($this->revisionKey && !$entity->{$this->defaultRevisionKey} && !empty($this->entityInfo['fieldable'])) {
        field_attach_update($this->entityType, $entity->original);
      }
    }

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