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
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | 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 ); } } |
Please login to continue.