function SqlContentEntityStorage::mapToStorageRecord
Same name in other branches
- 9 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::mapToStorageRecord()
- 8.9.x core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::mapToStorageRecord()
- 11.x 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 1036
Class
- SqlContentEntityStorage
- A content entity database storage implementation.
Namespace
Drupal\Core\Entity\SqlCode
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 = $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);
$empty_serial = empty($value) && $this->isColumnSerial($table_name, $schema_name);
// The user entity is a very special case where the ID field is a serial
// but we need to insert a row with an ID of 0 to represent the
// anonymous user.
// @todo https://drupal.org/i/3222123 implement a generic fix for all
// entity types.
$user_zero = $this->entityTypeId === 'user' && $value === 0;
if (!$empty_serial || $user_zero) {
$record->{$schema_name} = $value;
}
}
}
return $record;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.