You are here

public static function SqlContentEntityStorageSchema::castValue in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::castValue()

Typecasts values to proper datatypes.

MySQL PDO silently casts, e.g. FALSE and '' to 0, when inserting the value into an integer column, but PostgreSQL PDO does not. Use the schema information to correctly typecast the value.

@internal

Parameters

array $info: An array describing the schema field info. See hook_schema() and https://www.drupal.org/node/146843 for details.

mixed $value: The value to be converted.

Return value

mixed The converted value.

See also

hook_schema()

https://www.drupal.org/node/146843

3 calls to SqlContentEntityStorageSchema::castValue()
SqlContentEntityStorage::mapToStorageRecord in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php
Maps from an entity object to the storage record.
SqlContentEntityStorage::saveToDedicatedTables in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php
Saves values of fields that use dedicated tables.
SqlContentEntityStorageSchema::getSharedTableFieldSchema in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php
Gets the schema for a single field definition.

File

core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php, line 2563

Class

SqlContentEntityStorageSchema
Defines a schema handler that supports revisionable, translatable entities.

Namespace

Drupal\Core\Entity\Sql

Code

public static function castValue(array $info, $value) {

  // Preserve legal NULL values.
  if (isset($value) || !empty($info['not null'])) {
    if ($info['type'] === 'int' || $info['type'] === 'serial') {
      return (int) $value;
    }
    elseif ($info['type'] === 'float') {
      return (double) $value;
    }
    return (string) $value;
  }
  return $value;
}