Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::doSaveFieldItems()
  2. 9 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::doSaveFieldItems()

Writes entity field values to the storage.

This method is responsible for allocating entity and revision identifiers and updating the entity object with their values.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity object.

string[] $names: (optional) The name of the fields to be written to the storage. If an empty value is passed all field values are saved.

Overrides ContentEntityStorageBase::doSaveFieldItems

1 call to SqlContentEntityStorage::doSaveFieldItems()
UserStorage::doSaveFieldItems in core/modules/user/src/UserStorage.php
Writes entity field values to the storage.
1 method overrides SqlContentEntityStorage::doSaveFieldItems()
UserStorage::doSaveFieldItems in core/modules/user/src/UserStorage.php
Writes entity field values to the storage.

File

core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php, line 900

Class

SqlContentEntityStorage
A content entity database storage implementation.

Namespace

Drupal\Core\Entity\Sql

Code

protected function doSaveFieldItems(ContentEntityInterface $entity, array $names = []) {
  $full_save = empty($names);
  $update = !$full_save || !$entity
    ->isNew();
  if ($full_save) {
    $shared_table_fields = TRUE;
    $dedicated_table_fields = TRUE;
  }
  else {
    $table_mapping = $this
      ->getTableMapping();
    $shared_table_fields = FALSE;
    $dedicated_table_fields = [];

    // Collect the name of fields to be written in dedicated tables and check
    // whether shared table records need to be updated.
    foreach ($names as $name) {
      $storage_definition = $this->fieldStorageDefinitions[$name];
      if ($table_mapping
        ->allowsSharedTableStorage($storage_definition)) {
        $shared_table_fields = TRUE;
      }
      elseif ($table_mapping
        ->requiresDedicatedTableStorage($storage_definition)) {
        $dedicated_table_fields[] = $name;
      }
    }
  }

  // Update shared table records if necessary.
  if ($shared_table_fields) {
    $record = $this
      ->mapToStorageRecord($entity
      ->getUntranslated(), $this->baseTable);

    // Create the storage record to be saved.
    if ($update) {
      $default_revision = $entity
        ->isDefaultRevision();
      if ($default_revision) {
        $id = $record->{$this->idKey};

        // Remove the ID from the record to enable updates on SQL variants
        // that prevent updating serial columns, for example, mssql.
        unset($record->{$this->idKey});
        $this->database
          ->update($this->baseTable)
          ->fields((array) $record)
          ->condition($this->idKey, $id)
          ->execute();
      }
      if ($this->revisionTable) {
        if ($full_save) {
          $entity->{$this->revisionKey} = $this
            ->saveRevision($entity);
        }
        else {
          $record = $this
            ->mapToStorageRecord($entity
            ->getUntranslated(), $this->revisionTable);

          // Remove the revision ID from the record to enable updates on SQL
          // variants that prevent updating serial columns, for example,
          // mssql.
          unset($record->{$this->revisionKey});
          $entity
            ->preSaveRevision($this, $record);
          $this->database
            ->update($this->revisionTable)
            ->fields((array) $record)
            ->condition($this->revisionKey, $entity
            ->getRevisionId())
            ->execute();
        }
      }
      if ($default_revision && $this->dataTable) {
        $this
          ->saveToSharedTables($entity);
      }
      if ($this->revisionDataTable) {
        $new_revision = $full_save && $entity
          ->isNewRevision();
        $this
          ->saveToSharedTables($entity, $this->revisionDataTable, $new_revision);
      }
    }
    else {
      $insert_id = $this->database
        ->insert($this->baseTable, [
        'return' => Database::RETURN_INSERT_ID,
      ])
        ->fields((array) $record)
        ->execute();

      // Even if this is a new entity the ID key might have been set, in which
      // case we should not override the provided ID. An ID key that is not set
      // to any value is interpreted as NULL (or DEFAULT) and thus overridden.
      if (!isset($record->{$this->idKey})) {
        $record->{$this->idKey} = $insert_id;
      }
      $entity->{$this->idKey} = (string) $record->{$this->idKey};
      if ($this->revisionTable) {
        $record->{$this->revisionKey} = $this
          ->saveRevision($entity);
      }
      if ($this->dataTable) {
        $this
          ->saveToSharedTables($entity);
      }
      if ($this->revisionDataTable) {
        $this
          ->saveToSharedTables($entity, $this->revisionDataTable);
      }
    }
  }

  // Update dedicated table records if necessary.
  if ($dedicated_table_fields) {
    $names = is_array($dedicated_table_fields) ? $dedicated_table_fields : [];
    $this
      ->saveToDedicatedTables($entity, $update, $names);
  }
}