public function DrupalCommerceEntityController::save in Commerce Core 7
Permanently saves the given entity.
In case of failures, an exception is thrown.
Parameters
$entity: The entity to save.
$transaction: An optional transaction object to pass thru. If passed the caller is responsible for rolling back the transaction if something goes wrong.
Return value
SAVED_NEW or SAVED_UPDATED depending on the operation performed.
Overrides EntityAPIControllerInterface::save
5 calls to DrupalCommerceEntityController::save()
- CommerceCustomerProfileEntityController::save in modules/
customer/ includes/ commerce_customer_profile.controller.inc - Saves a customer profile.
- CommerceLineItemEntityController::save in modules/
line_item/ includes/ commerce_line_item.controller.inc - Saves a line item.
- CommerceOrderEntityController::save in modules/
order/ includes/ commerce_order.controller.inc - Saves an order.
- CommercePaymentTransactionEntityController::save in modules/
payment/ includes/ commerce_payment_transaction.controller.inc - Saves a payment transaction.
- CommerceProductEntityController::save in modules/
product/ includes/ commerce_product.controller.inc - Saves a product.
5 methods override DrupalCommerceEntityController::save()
- CommerceCustomerProfileEntityController::save in modules/
customer/ includes/ commerce_customer_profile.controller.inc - Saves a customer profile.
- CommerceLineItemEntityController::save in modules/
line_item/ includes/ commerce_line_item.controller.inc - Saves a line item.
- CommerceOrderEntityController::save in modules/
order/ includes/ commerce_order.controller.inc - Saves an order.
- CommercePaymentTransactionEntityController::save in modules/
payment/ includes/ commerce_payment_transaction.controller.inc - Saves a payment transaction.
- CommerceProductEntityController::save in modules/
product/ includes/ commerce_product.controller.inc - Saves a product.
File
- includes/
commerce.controller.inc, line 213 - Provides a central controller for Drupal Commerce.
Class
- DrupalCommerceEntityController
- Default implementation of DrupalCommerceEntityControllerInterface.
Code
public function save($entity, DatabaseTransaction $transaction = NULL) {
if (!isset($transaction)) {
$transaction = db_transaction();
$started_transaction = TRUE;
}
try {
// Load the stored entity, if any. If this save was invoked during a
// previous save's insert or update hook, this means the $entity->original
// value already set on the entity will be replaced with the entity as
// saved. This will allow any original entity comparisons in the current
// save process to react to the most recently saved version of the entity.
if (!empty($entity->{$this->idKey}) && !isset($entity->original)) {
$this->unchangedEntities[$entity->{$this->idKey}] = TRUE;
// 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});
unset($this->unchangedEntities[$entity->{$this->idKey}]);
}
$this
->invoke('presave', $entity);
// When saving a new revision, unset any existing revision ID so as to
// ensure that a new revision will actually be created, then store the old
// revision ID in a separate property for use by hook implementations.
if (!empty($this->revisionKey) && empty($entity->is_new) && !empty($entity->revision) && !empty($entity->{$this->revisionKey})) {
$entity->old_revision_id = $entity->{$this->revisionKey};
unset($entity->{$this->revisionKey});
}
if (empty($entity->{$this->idKey}) || !empty($entity->is_new)) {
// For new entities, create the row in the base table, then save the
// revision.
$op = 'insert';
$return = drupal_write_record($this->entityInfo['base table'], $entity);
if (!empty($this->revisionKey)) {
drupal_write_record($this->entityInfo['revision table'], $entity);
$update_base_table = TRUE;
}
}
else {
$op = 'update';
$return = drupal_write_record($this->entityInfo['base table'], $entity, $this->idKey);
if (!empty($this->revisionKey)) {
if (!empty($entity->revision)) {
drupal_write_record($this->entityInfo['revision table'], $entity);
$update_base_table = TRUE;
}
else {
drupal_write_record($this->entityInfo['revision table'], $entity, $this->revisionKey);
}
}
}
if (!empty($update_base_table)) {
// Go back to the base table and update the pointer to the revision ID.
db_update($this->entityInfo['base table'])
->fields(array(
$this->revisionKey => $entity->{$this->revisionKey},
))
->condition($this->idKey, $entity->{$this->idKey})
->execute();
}
// Update the static cache so that the next entity_load() will return this
// newly saved entity.
$this->entityCache[$entity->{$this->idKey}] = $entity;
// Maintain the list of locked entities and release the lock if possible.
unset($this->lockedEntities[$entity->{$this->idKey}]);
$this
->releaseLock();
$this
->invoke($op, $entity);
// Ignore slave server temporarily.
db_ignore_slave();
// We unset the original version of the entity after the current save as
// it no longer accurately represents the version of the entity saved in
// the database. However, if this save was invoked during a previous
// save's insert or update hook, this means that any hook implementations
// executing after this save will no longer have an original version of
// the entity to compare against. Attempting to compare against the non-
// existent original entity in code or Rules will result in an error.
unset($entity->original);
unset($entity->is_new);
unset($entity->revision);
return $return;
} catch (Exception $e) {
if (!empty($started_transaction)) {
$transaction
->rollback();
watchdog_exception($this->entityType, $e);
}
throw $e;
}
}