function SqlContentEntityStorageSchema::castValue

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::castValue()
  2. 10 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::castValue()
  3. 11.x core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::castValue()

Typecasts values to the proper data type.

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

4 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.
SqlContentEntityStorageSchemaTest::testCastValue in core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php
Tests various value casts depending on column schema.

File

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

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.