You are here

protected function ContentEntityStorage::doSave in MongoDB 8

Performs storage-specific saving of the entity.

Parameters

int|string $id: The original entity ID.

\Drupal\Core\Entity\EntityInterface $entity: The entity to save.

Return value

bool|int If the record insert or update failed, returns FALSE. If it succeeded, returns SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides ContentEntityStorageBase::doSave

1 call to ContentEntityStorage::doSave()
MongodbUserStorage::doSave in mongodb_user/src/MongodbUserStorage.php
Performs storage-specific saving of the entity.
1 method overrides ContentEntityStorage::doSave()
MongodbUserStorage::doSave in mongodb_user/src/MongodbUserStorage.php
Performs storage-specific saving of the entity.

File

src/Entity/ContentEntityStorage.php, line 174
Contains Drupal\mongodb\Entity\ContentEntityStorage.

Class

ContentEntityStorage

Namespace

Drupal\mongodb\Entity

Code

protected function doSave($id, EntityInterface $entity) {

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $id = $entity
    ->id();
  $is_new = FALSE;
  if ($entity
    ->isNew() || $id === NULL || $id === '') {
    $is_new = TRUE;
    if ($id === NULL || $id === '') {
      $entity
        ->get($this->entityType
        ->getKey('id'))->value = $this
        ->nextId('entity', $id);
    }
  }
  if ($this->entityType
    ->isRevisionable()) {
    $revision_key = $this->entityType
      ->getKey('revision');
    if ($entity
      ->isNewRevision() && !$entity
      ->getRevisionId()) {
      $entity
        ->get($revision_key)->value = $this
        ->nextId('entity_revision');
    }
  }
  $this
    ->invokeFieldMethod($is_new ? 'insert' : 'update', $entity);
  $data = $this
    ->getDataToSave($entity);
  if ($entity
    ->isDefaultRevision()) {
    $criteria['_id'] = $entity
      ->id();
    if ($this->entityType
      ->isRevisionable()) {
      $data['revision_id'] = $entity
        ->get($revision_key)->value;
    }
    $collection = $this
      ->mongoCollection($entity, 'entity');
    $collection
      ->update($criteria, $criteria + $data, [
      'upsert' => TRUE,
    ]);
    $return = $collection->db
      ->lastError();
  }
  if ($this->entityType
    ->isRevisionable()) {
    $criteria['_id'] = $entity
      ->getRevisionId();
    $criteria['entity_id'] = $entity
      ->id();
    $collection = $this
      ->mongoCollection($entity, 'entity_revision');
    $collection
      ->update($criteria, $criteria + $data, [
      'upsert' => TRUE,
    ]);
    $entity
      ->setNewRevision(FALSE);
    if (!isset($return)) {
      $return = $collection->db
        ->lastError();
    }
  }
  if (!$is_new) {
    $this
      ->invokeTranslationHooks($entity);
  }
  if (isset($collection) && empty($return['err'])) {
    return $return['updatedExisting'] ? SAVED_UPDATED : SAVED_NEW;
  }
  return FALSE;
}