You are here

public function WSEntityAPIController::save in Web Service Data 7

Implements EntityAPIControllerInterface.

Parameters

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

Overrides EntityAPIController::save

File

modules/wsentities/includes/wsentity.controller.inc, line 101
Provides a controller building upon the core controller but providing more features like full CRUD functionality.

Class

WSEntityAPIController
WSEntityAPIController extends and overrides most EntityAPIController features

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});
    }
    $this
      ->invoke('presave', $entity);
    if (!empty($entity->{$this->idKey}) && empty($entity->is_new)) {

      /*
        TODO: WS PUT
              $return = drupal_write_record($this->entityInfo['base table'], $entity, $this->idKey);
      */
      $this
        ->resetCache(array(
        $entity->{$this->idKey},
      ));
      $this
        ->invoke('update', $entity);
    }
    else {

      /*
        TODO: WS POST
              $return = drupal_write_record($this->entityInfo['base table'], $entity);
      */
      $this
        ->invoke('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 $e;
  }
}