You are here

protected function SqlContentEntityStorage::mapToStorageRecord in Drupal 8

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

Maps from an entity object to the storage record.

Parameters

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

string $table_name: (optional) The table name to map records to. Defaults to the base table.

Return value

object The record to store.

4 calls to SqlContentEntityStorage::mapToStorageRecord()
SqlContentEntityStorage::doSaveFieldItems in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php
Writes entity field values to the storage.
SqlContentEntityStorage::mapToDataStorageRecord in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php
Maps from an entity object to the storage record of the field data.
SqlContentEntityStorage::restore in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php
Restores a previously saved entity.
SqlContentEntityStorage::saveRevision in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php
Saves an entity revision.

File

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

Class

SqlContentEntityStorage
A content entity database storage implementation.

Namespace

Drupal\Core\Entity\Sql

Code

protected function mapToStorageRecord(ContentEntityInterface $entity, $table_name = NULL) {
  if (!isset($table_name)) {
    $table_name = $this->baseTable;
  }
  $record = new \stdClass();
  $table_mapping = $this
    ->getTableMapping();
  foreach ($table_mapping
    ->getFieldNames($table_name) as $field_name) {
    if (empty($this->fieldStorageDefinitions[$field_name])) {
      throw new EntityStorageException("Table mapping contains invalid field {$field_name}.");
    }
    $definition = $this->fieldStorageDefinitions[$field_name];
    $columns = $table_mapping
      ->getColumnNames($field_name);
    foreach ($columns as $column_name => $schema_name) {

      // If there is no main property and only a single column, get all
      // properties from the first field item and assume that they will be
      // stored serialized.
      // @todo Give field types more control over this behavior in
      //   https://www.drupal.org/node/2232427.
      if (!$definition
        ->getMainPropertyName() && count($columns) == 1) {
        $value = ($item = $entity->{$field_name}
          ->first()) ? $item
          ->getValue() : [];
      }
      else {
        $value = isset($entity->{$field_name}->{$column_name}) ? $entity->{$field_name}->{$column_name} : NULL;
      }
      if (!empty($definition
        ->getSchema()['columns'][$column_name]['serialize'])) {
        $value = serialize($value);
      }

      // Do not set serial fields if we do not have a value. This supports all
      // SQL database drivers.
      // @see https://www.drupal.org/node/2279395
      $value = SqlContentEntityStorageSchema::castValue($definition
        ->getSchema()['columns'][$column_name], $value);
      if (!(empty($value) && $this
        ->isColumnSerial($table_name, $schema_name))) {
        $record->{$schema_name} = $value;
      }
    }
  }
  return $record;
}