SqlContentEntityStorage::doSaveFieldItems

protected SqlContentEntityStorage::doSaveFieldItems(ContentEntityInterface $entity, array $names = [])

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

File

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

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();
    $storage_definitions = $this->entityManager->getFieldStorageDefinitions($this->entityTypeId);
    $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 = $storage_definitions[$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) {
        $this->database
          ->update($this->baseTable)
          ->fields((array) $record)
          ->condition($this->idKey, $record->{$this->idKey})
          ->execute();
      }
      if ($this->revisionTable) {
        if ($full_save) {
          $entity->{$this->revisionKey} = $this->saveRevision($entity);
        }
        else {
          $record = $this->mapToStorageRecord($entity->getUntranslated(), $this->revisionTable);
          $entity->preSaveRevision($this, $record);
          $this->database
            ->update($this->revisionTable)
            ->fields((array) $record)
            ->condition($this->revisionKey, $record->{$this->revisionKey})
            ->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, array('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);
  }
}
doc_Drupal
2016-10-29 09:43:22
Comments
Leave a Comment

Please login to continue.