SqlContentEntityStorage::mapToStorageRecord

protected SqlContentEntityStorage::mapToStorageRecord(ContentEntityInterface $entity, $table_name = NULL)

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

\stdClass The record to store.

File

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

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->getFieldStorageDefinitions()[$field_name])) {
      throw new EntityStorageException("Table mapping contains invalid field $field_name.");
    }
    $definition = $this->getFieldStorageDefinitions()[$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() : array();
      }
      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 = drupal_schema_get_field_value($definition->getSchema()['columns'][$column_name], $value);
      if (!(empty($value) && $this->isColumnSerial($table_name, $schema_name))) {
        $record->$schema_name = $value;
      }
    }
  }

  return $record;
}
doc_Drupal
2016-10-29 09:43:26
Comments
Leave a Comment

Please login to continue.