You are here

public function CommerceFileLicenseLogEntityController::save in Commerce File 7

Implements EntityAPIControllerInterface::save()

Parameters

$entity: The full entity object to save.

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

Return value

The saved entity object.

Overrides EntityAPIController::save

File

includes/commerce_file_license_log.controller.inc, line 34
The controller for the Commerce File License Log entity containing the CRUD operations.

Class

CommerceFileLicenseLogEntityController
@file The controller for the Commerce File License Log entity containing the CRUD operations.

Code

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

  // determine is_new
  $is_new = TRUE;
  if (!empty($entity->{$this->idKey})) {

    // if have an id, always update
    unset($entity->is_new);
    $is_new = FALSE;
  }
  elseif (isset($entity->is_new)) {

    // let entity tell us if we're new
    $is_new = $entity->is_new;
  }
  try {

    // set properties for new entities
    if ($is_new) {
      $entity->timestamp = REQUEST_TIME;
    }

    // clone for save so that we dont alter entity object to the serialized arrays
    $clone = clone $entity;
    $return = parent::save($clone, $transaction);

    // alter actual entity after successful save
    if ($return) {
      unset($entity->is_new);
      unset($entity->original);

      // add id
      if ($is_new) {
        $entity->{$this->idKey} = $clone->{$this->idKey};
      }
    }
    return $return;
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog_exception($this->entityType, $e);
    throw $e;
  }
}