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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
      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.
      $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
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.