diff --git a/src/Mapping/DefaultTypedFieldMapper.php b/src/Mapping/DefaultTypedFieldMapper.php index 47b842cf496..61894e93634 100644 --- a/src/Mapping/DefaultTypedFieldMapper.php +++ b/src/Mapping/DefaultTypedFieldMapper.php @@ -5,6 +5,7 @@ namespace Doctrine\ORM\Mapping; use BackedEnum; +use BcMath\Number; use DateInterval; use DateTime; use DateTimeImmutable; @@ -40,7 +41,12 @@ final class DefaultTypedFieldMapper implements TypedFieldMapper /** @param array|string> $typedFieldMappings */ public function __construct(array $typedFieldMappings = []) { - $this->typedFieldMappings = array_merge(self::DEFAULT_TYPED_FIELD_MAPPINGS, $typedFieldMappings); + $defaultMappings = self::DEFAULT_TYPED_FIELD_MAPPINGS; + if (defined(Types::class . '::NUMBER')) { // DBAL 4.3+ + $defaultMappings[Number::class] = Types::NUMBER; + } + + $this->typedFieldMappings = array_merge($defaultMappings, $typedFieldMappings); } /** diff --git a/tests/Tests/Models/TypedProperties/UserTyped.php b/tests/Tests/Models/TypedProperties/UserTyped.php index 22ac2127ac2..f4ff01a027d 100644 --- a/tests/Tests/Models/TypedProperties/UserTyped.php +++ b/tests/Tests/Models/TypedProperties/UserTyped.php @@ -4,6 +4,7 @@ namespace Doctrine\Tests\Models\TypedProperties; +use BcMath\Number; use DateInterval; use DateTime; use DateTimeImmutable; @@ -54,6 +55,9 @@ class UserTyped #[ORM\Embedded] public Contact|null $contact = null; + #[ORM\Column(precision: 5, scale: 2)] + public Number|null $bodyHeight = null; + public static function loadMetadata(ClassMetadata $metadata): void { $metadata->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_NONE); diff --git a/tests/Tests/ORM/Mapping/ClassMetadataTest.php b/tests/Tests/ORM/Mapping/ClassMetadataTest.php index 296c655d743..8d8957216cc 100644 --- a/tests/Tests/ORM/Mapping/ClassMetadataTest.php +++ b/tests/Tests/ORM/Mapping/ClassMetadataTest.php @@ -58,6 +58,7 @@ use function assert; use function class_exists; use function count; +use function defined; use function serialize; use function str_contains; use function str_replace; @@ -197,6 +198,12 @@ public function testFieldTypeFromReflection(): void // float $cm->mapField(['fieldName' => 'float']); self::assertEquals('float', $cm->getTypeOfField('float')); + + // number, requires DBAL 4.3+ + if (defined(Types::class . '::NUMBER')) { + $cm->mapField(['fieldName' => 'bodyHeight']); + self::assertEquals('number', $cm->getTypeOfField('bodyHeight')); + } } #[TestGroup('GH10313')] diff --git a/tests/Tests/ORM/Mapping/TypedFieldMapperTest.php b/tests/Tests/ORM/Mapping/TypedFieldMapperTest.php index 3f8c0863ca0..6aaed387d9d 100644 --- a/tests/Tests/ORM/Mapping/TypedFieldMapperTest.php +++ b/tests/Tests/ORM/Mapping/TypedFieldMapperTest.php @@ -11,10 +11,13 @@ use Doctrine\Tests\Models\TypedProperties\UserTyped; use Doctrine\Tests\ORM\Mapping\TypedFieldMapper\CustomIntAsStringTypedFieldMapper; use Doctrine\Tests\OrmTestCase; +use Generator; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use ReflectionClass; +use function defined; + #[Group('GH10313')] class TypedFieldMapperTest extends OrmTestCase { @@ -36,7 +39,7 @@ private static function chainTypedFieldMapper(): ChainTypedFieldMapper /** * Data Provider for NamingStrategy#classToTableName * - * @return array< + * @return Generator< * array{ * TypedFieldMapper, * ReflectionClass, @@ -44,28 +47,30 @@ private static function chainTypedFieldMapper(): ChainTypedFieldMapper * array{fieldName: string, enumType?: string, type?: mixed} * }> */ - public static function dataFieldToMappedField(): array + public static function dataFieldToMappedField(): Generator { $reflectionClass = new ReflectionClass(UserTyped::class); - return [ - // DefaultTypedFieldMapper - [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'id'], ['fieldName' => 'id', 'type' => Types::INTEGER]], - [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'username'], ['fieldName' => 'username', 'type' => Types::STRING]], - [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'dateInterval'], ['fieldName' => 'dateInterval', 'type' => Types::DATEINTERVAL]], - [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'dateTime'], ['fieldName' => 'dateTime', 'type' => Types::DATETIME_MUTABLE]], - [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'dateTimeImmutable'], ['fieldName' => 'dateTimeImmutable', 'type' => Types::DATETIME_IMMUTABLE]], - [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'array'], ['fieldName' => 'array', 'type' => Types::JSON]], - [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'boolean'], ['fieldName' => 'boolean', 'type' => Types::BOOLEAN]], - [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'float'], ['fieldName' => 'float', 'type' => Types::FLOAT]], + // DefaultTypedFieldMapper + yield [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'id'], ['fieldName' => 'id', 'type' => Types::INTEGER]]; + yield [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'username'], ['fieldName' => 'username', 'type' => Types::STRING]]; + yield [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'dateInterval'], ['fieldName' => 'dateInterval', 'type' => Types::DATEINTERVAL]]; + yield [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'dateTime'], ['fieldName' => 'dateTime', 'type' => Types::DATETIME_MUTABLE]]; + yield [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'dateTimeImmutable'], ['fieldName' => 'dateTimeImmutable', 'type' => Types::DATETIME_IMMUTABLE]]; + yield [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'array'], ['fieldName' => 'array', 'type' => Types::JSON]]; + yield [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'boolean'], ['fieldName' => 'boolean', 'type' => Types::BOOLEAN]]; + yield [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'float'], ['fieldName' => 'float', 'type' => Types::FLOAT]]; + + if (defined(Types::class . '::NUMBER')) { + yield [self::defaultTypedFieldMapper(), $reflectionClass, ['fieldName' => 'bodyHeight'], ['fieldName' => 'bodyHeight', 'type' => Types::NUMBER]]; + } - // CustomIntAsStringTypedFieldMapper - [self::customTypedFieldMapper(), $reflectionClass, ['fieldName' => 'id'], ['fieldName' => 'id', 'type' => Types::STRING]], + // CustomIntAsStringTypedFieldMapper + yield [self::customTypedFieldMapper(), $reflectionClass, ['fieldName' => 'id'], ['fieldName' => 'id', 'type' => Types::STRING]]; - // ChainTypedFieldMapper - [self::chainTypedFieldMapper(), $reflectionClass, ['fieldName' => 'id'], ['fieldName' => 'id', 'type' => Types::STRING]], - [self::chainTypedFieldMapper(), $reflectionClass, ['fieldName' => 'username'], ['fieldName' => 'username', 'type' => Types::STRING]], - ]; + // ChainTypedFieldMapper + yield [self::chainTypedFieldMapper(), $reflectionClass, ['fieldName' => 'id'], ['fieldName' => 'id', 'type' => Types::STRING]]; + yield [self::chainTypedFieldMapper(), $reflectionClass, ['fieldName' => 'username'], ['fieldName' => 'username', 'type' => Types::STRING]]; } /**