You are here

public function CommerceFileLicenseEntityController::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.controller.inc, line 45
The controller for the File License entity containing the CRUD operations.

Class

CommerceFileLicenseEntityController
@file The controller for the File License 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;
  }
  else {

    // default to new
    $is_new = TRUE;
    $entity->is_new = TRUE;
  }
  try {
    $entity->changed = REQUEST_TIME;
    if ($is_new) {

      // Set properties for new entities
      if (!isset($entity->created) || empty($entity->created)) {
        $entity->created = REQUEST_TIME;
      }
    }

    // set initial granted time
    if (empty($entity->granted) && $entity
      ->is_allowed()) {
      $entity->granted = 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;
  }
}